2012-11-08 5 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(); 
    } 

    }` 

로그 캣 : 당신은

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

를 호출

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

스택 추적, 안드로이드, 전화 모델 등을 게시하십시오. – thedayofcondor

+0

멋진 코딩 스타일 : '문자열 a, b, c, d, e;' – WarrenFaith

+0

무엇이 스택 추적입니까? 버전 4.1.1 삼성 갤럭시 s3 –

답변

0

두 번째 인수가 누락되었습니다. 다음

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

는 또한 보일러 프로젝트 here를 참조 for example

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

보십시오.

+0

고마워! 내가 계류중인 의도를 놓치고 있다는 것을 깨닫지 못했습니다. –

관련 문제