0

不确定我的应用程序是否能够编写我从应用程序用户那里获得的信息。当我按下写入标签按钮时,应用程序不断崩溃。我通常有 5 个EditText用于用户输入。然后我将使用这些值并将其制成一个字符串以写入 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();
    }

    }`

日志猫:

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);

缺少第二个论点。例如尝试

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 回答