0

アプリのユーザーから取得した情報をアプリが書き込めるかどうかわからない。タグへの書き込みボタンを押すと、アプリがクラッシュし続けます。私は通常EditText、ユーザー入力用に5つあります。次に、これらの値を使用して、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)
4

1 に答える 1

0

あなたが呼んでいます

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

2番目の引数がありません。たとえば、試してみてください

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

それから

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

ボイラープレート プロジェクトも参照してください

于 2012-11-08T13:53:31.197 に答える