2012-11-08 97 views
0

不知道我的應用程序是否能夠寫入我從應用程序用戶處獲得的信息。當我按下寫入標籤按鈕時,應用程序不斷崩潰。我通常有5 EditText供用戶輸入。然後,我將使用這些值並將其製作成字符串以寫入NFC標籤。 .`(此信息將然後使用一個單獨的應用程序,然後將再次打破字符串轉換爲不同的組件讀取)NFC標籤寫入應用程序不斷崩潰

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

    setContentView(R.layout.write_tag1); 

    findViewById(R.id.nfc_write_confirm).setOnClickListener(mTagWriter); 
    cancel3Button = (Button)findViewById(R.id.nfc_write_cancel); 
    foodName = (EditText)findViewById(R.id.nfc_food_name); 
    protValue = (EditText)findViewById(R.id.nfc_protein_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); 
    allValue = (TextView)findViewById(R.id.nfc_all_value); 

    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    mWriteTagFilters = new IntentFilter[] { 
      tagDetected 
      }; 

    cancel3Button.setOnClickListener(new Button.OnClickListener(){ 

     public void onClick(View v) { 


      finish(); 

     } 
     }); 


     }  

     @Override 
     protected void onNewIntent(Intent intent) { 

     if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
      Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      writeTag(getNoteAsNdef(), detectedTag); 
     } 

    } 

    private View.OnClickListener mTagWriter = new View.OnClickListener() { 

    public void onClick(View v) { 
    // Write to a tag for as long as the dialog is shown. 
    enableTagWriteMode(); 

    new AlertDialog.Builder(NFCWriteTag1.this).setTitle("Touch tag to write") 
      .setOnCancelListener(new DialogInterface.OnCancelListener() { 

       public void onCancel(DialogInterface dialog) { 
        disableTagWriteMode(); 
       } 
      }).create().show(); 
    } 
    }; 


    private NdefMessage getNoteAsNdef() { 
String a, b, c, d, e; 
a = (foodName.getText() + ",").toString(); 
b = (protValue.getText() + ",").toString(); 
c = (carbValue.getText() + ",").toString(); 
d = (fatValue.getText() + ",").toString(); 
e = energyValue.getText().toString(); 
allValue.setText(a+b+c+d+e); 
    byte[] textBytes = allValue.getText().toString().getBytes(); 
    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(), 
     new byte[] {}, textBytes); 
    return new NdefMessage(new NdefRecord[] { 
    textRecord 
    }); 
    } 

    private void enableTagWriteMode() { 
    mWriteMode = true; 
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    mWriteTagFilters = new IntentFilter[] { 
    tagDetected 
    }; 
    mNfcAdapter.enableForegroundDispatch(this, null, mWriteTagFilters, null); 
    } 

    private void disableTagWriteMode() { 
    mWriteMode = false; 
    mNfcAdapter.disableForegroundDispatch(this); 
    } 

    boolean writeTag(NdefMessage message, Tag tag) { 
    int size = message.toByteArray().length; 

    try { 
    Ndef ndef = Ndef.get(tag); 
    if (ndef != null) { 
     ndef.connect(); 

     if (!ndef.isWritable()) { 
      toast("Tag is read-only."); 
      return false; 
     } 
     if (ndef.getMaxSize() < size) { 
      toast("Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size 
        + " bytes."); 
      return false; 
     } 

     ndef.writeNdefMessage(message); 
     toast("Wrote message to pre-formatted tag."); 
     return true; 
    } else { 
     NdefFormatable format = NdefFormatable.get(tag); 
     if (format != null) { 
      try { 
       format.connect(); 
       format.format(message); 
       toast("Formatted tag and wrote message"); 
       return true; 
      } catch (IOException e) { 
       toast("Failed to format tag."); 
       return false; 
      } 
     } else { 
      toast("Tag doesn't support NDEF."); 
      return false; 
     } 
    } 
    } catch (Exception e) { 
    toast("Failed to write tag"); 
    } 

    return false; 
    } 

    private void toast(String text) { 
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 
    } 

    }` 

的logcat:

11-08 18:29:18.745: E/AndroidRuntime(23252): FATAL EXCEPTION: main 
11-08 18:29:18.745: E/AndroidRuntime(23252): java.lang.NullPointerException 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1079) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at com.nfc.tag.writing.mealplan.NFCWriteTag1.enableTagWriteMode(NFCWriteTag1.java:128) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at com.nfc.tag.writing.mealplan.NFCWriteTag1.access$0(NFCWriteTag1.java:122) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at com.nfc.tag.writing.mealplan.NFCWriteTag1$1.onClick(NFCWriteTag1.java:93) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.view.View.performClick(View.java:4211) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.view.View$PerformClick.run(View.java:17267) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.os.Handler.handleCallback(Handler.java:615) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.os.Looper.loop(Looper.java:137) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at android.app.ActivityThread.main(ActivityThread.java:4898) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at java.lang.reflect.Method.invokeNative(Native Method) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at java.lang.reflect.Method.invoke(Method.java:511) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
11-08 18:29:18.745: E/AndroidRuntime(23252): at dalvik.system.NativeStart.main(Native Method) 
+1

請發佈堆棧跟蹤,Android版本,手機型號等 – thedayofcondor

+0

不錯的編碼風格:'String a,b,c,d,e;' – WarrenFaith

+0

對不起什麼是堆棧跟蹤?版本4.1.1 samsung galaxy s3 –

回答

0

要調用

mNfcAdapter.enableForegroundDispatch(this, null, mWriteTagFilters, null); 

這是缺少第二個參數。嘗試for example

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

然後

mNfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mWriteTagFilters, null); 

也看到了樣板工程here

+0

謝謝!沒有意識到我錯過了待決意圖。 –