0
我正在研究一個應用,該應用將接收來自標記的輸入並將值插入特定變量(仍在工作)。但是,當我在手機附近放置預設的NFC標籤時,應用程序會從標籤中獲取信息。相反,它會啓動應用程序,然後立即崩潰。當我啓動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;
}
}`
請分享logcat日誌.. –
java.lang.RuntimeException:無法啓動活動ComponentInfo {org.project.MealPlan/org.project.MealPlan .NFCAdding}:java.lang.ClassCastException:android.widget.TextView無法轉換爲android.widget.EditText'像這樣?對不起,我在這裏是新的.. –
看起來像你的EditText屬性之一是在佈局xml中定義爲TextView。 – rgrocha