我有一个使用P2P NFC的android应用程序。 NFC工作,但我必须点击两次设备才能启动它。当我调试我的应用程序并点击设备时,它会调用createNdefMessage函数,但会在JavaBinder中引发异常。 在运行模式下,它不会崩溃,但我必须点击两次设备才能启动NFC。Android NFC抛出异常
在NFC之前我调用文件选择器来选择要传输的文件。 这里是我的代码的onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// INFO TEXTVIEW
mInfoText = (TextView) findViewById(R.id.info_text_view);
// FILE SELECTOR BUTTON
mStartActivityButton = (Button)findViewById(R.id.start_file_picker_button);
mStartActivityButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.start_file_picker_button:
// Create a new Intent for the file picker activity
Intent intent = new Intent(getApplicationContext(), FilePickerActivity.class);
// Start the activity
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
});
// CHECK FOR AVAILABLE NFC ADAPTOR
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
mInfoText.setText("NFC is not available on this device.");
} else {
// Register callback to set NDEF message
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Register callback to listen for message-sent success
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
}
}
createNdefMessage
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
mInfoText.setTextColor(Color.WHITE);
mInfoText.setText("File Transfer In Progress ...");
NdefMessage msg = new NdefMessage(NdefRecord.createMime(
"application/com.example.android.beam", text.getBytes()));
return msg;
}
我的应用程序得来的
mInfoText.setTextColor(Color.WHITE);
一个例外是不是因为我有两个目的?
post stacktrace之前,请试着用
findViewById()
进行初始化。 – njzk2