0

我正在开发一个应用程序,该应用程序将从标签中获取输入并将值插入特定变量(仍在处理中)。但是,当我在手机附近放置一个预设的 NFC 标签时,应用程序会从标签中获取信息。相反,它会启动应用程序,然后立即崩溃。

`公共类NFCAdding扩展活动{

private static final String TAG = "MealPlan";
private EditText foodName, protValue, carbValue, fatValue, energyValue;
private Button addBreak, addLunch, addDinner;
NfcAdapter mNfcAdapter;

private boolean mResumed = false;

PendingIntent mNfcPendingIntent;
IntentFilter[] mNdefExchangeFilters;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nfc_add);

    foodName = (EditText)findViewById(R.id.nfc_food_name);
    protValue = (EditText)findViewById(R.id.nfc_prot_value);
    carbValue = (EditText)findViewById(R.id.nfc_carb_value);
    fatValue = (EditText)findViewById(R.id.nfc_fat_value);
    energyValue = (EditText)findViewById(R.id.nfc_energy_value);
    addBreak = (Button)findViewById(R.id.nfc_add_breakfast);
    addLunch = (Button)findViewById(R.id.nfc_add_lunch);
    addDinner = (Button)findViewById(R.id.nfc_add_dinner);

    mNfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Intent filters for reading a note from a tag or exchanging over p2p.
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndefDetected.addDataType("text/plain");
    } catch (MalformedMimeTypeException e) { }
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected };

    addBreak.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v) {
             Intent intent = new Intent();
     intent.setClass(getApplicationContext(), AddItem.class);
     startActivity(intent);
     finish();                                               
     }

});

    addLunch.setOnClickListener(new Button.OnClickListener(){
     public void onClick(View v) {
            Intent intent = new Intent();
    intent.setClass(getApplicationContext(), LunchAdd.class);
    startActivity(intent);
    finish();                                               
    }

});

    addDinner.setOnClickListener(new Button.OnClickListener(){
     public void onClick(View v) {
            Intent intent = new Intent();
    intent.setClass(getApplicationContext(), DinnerAdd.class);
    startActivity(intent);
    finish();                                               
    }

});

}

 @Override
    protected void onResume() {
        super.onResume();
        mResumed = true;
        // Sticky notes received from Android
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            NdefMessage[] messages = getNdefMessages(getIntent());
            byte[] payload = messages[0].getRecords()[0].getPayload();
            setNoteBody(new String(payload));
            setIntent(new Intent()); // Consume this intent.
        }
        enableNdefExchangeMode();
    }

 private void setNoteBody(String body) {
        Editable text = foodName.getText();
        text.clear();
        text.append(body);
    }

 private void enableNdefExchangeMode() {
        mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mNdefExchangeFilters, null);
    }

    private void disableNdefExchangeMode() {
        mNfcAdapter.disableForegroundDispatch(this);
    }

NdefMessage[] getNdefMessages(Intent intent) {
    // Parse the intent
    NdefMessage[] msgs = null;
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        } else {
            // Unknown tag type
            byte[] empty = new byte[] {};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[] {
                record
            });
            msgs = new NdefMessage[] {
                msg
            };
        }
    } else {
        Log.d(TAG, "Unknown intent.");
        finish();
    }
    return msgs;
}

}`

4

1 回答 1

0

根据您的日志,以下之一不是 EditText,而是 TextView:

foodName = (EditText)findViewById(R.id.nfc_food_name);
protValue = (EditText)findViewById(R.id.nfc_prot_value);
carbValue = (EditText)findViewById(R.id.nfc_carb_value);
fatValue = (EditText)findViewById(R.id.nfc_fat_value);
energyValue = (EditText)findViewById(R.id.nfc_energy_value);

检查带有 ids 的元素nfc_food_name, nfc_prot_value, nfc_carb_value, nfc_fat_value and nfc_energy_value。看起来其中一个或多个是 TextView 而不是 EditText..

于 2012-11-08T10:20:42.807 回答