0

我有一个使用 P2P NFC 的 android 应用程序。NFC 有效,但我必须轻按设备两次才能启动它。当我调试我的应用程序并点击设备时,它会调用 createNdefMessage 函数,但会在 JavaBinder 处引发异常。在运行模式下它不会崩溃,但我必须点击设备两次才能启动 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);
    }

}

创建NdefMessage

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);

是因为我有两个意图吗?

4

1 回答 1

0

我认为当你打电话时mInfoText.setTextColor(Color.WHITE) mInfoText是空的。findViewById()在打电话之前尝试初始化setTextColor

于 2013-06-04T14:23:39.897 回答