自定义NFC应用程序不断崩溃



我有一个非常具体的问题。我写了一个向NFC芯片发送信息的应用程序,但当我按下按钮时,它一直崩溃。我使用的编辑器Eclipse没有给出任何错误或警告,所以它不是代码中的错误。

我觉得非常非常奇怪的是,尽管try/catch块包含了所有代码,但它还是崩溃了。它启动并检查NFC是否启用的部分工作正常,但按下"写入"只会使应用程序崩溃:

应用程序Tag Writer(process com.harold.Tag.Writer)已意外停止。

你们能帮我吗??

如果您需要其他信息,请询问。

package com.harold.tag.writer;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.common.base.Charsets;
import com.google.common.primitives.Bytes;
public class Main extends Activity implements OnClickListener {
EditText etUser;
Button bWrite;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialise();
}
private void initialise(){
    nfcsettings();
    etUser = (EditText)findViewById(R.id.etTag);
    bWrite = (Button)findViewById(R.id.bWrite);
    bWrite.setOnClickListener(this);
}
public void onClick(View v) {
    try
    {
        //initialize nfc part
        nfcsettings();
        Intent intent = this.getIntent();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        writeTag(tag);   
    }
    catch (Exception e){
        Toast.makeText(getBaseContext(), "Button clicking", Toast.LENGTH_SHORT).show();
    }
}
private void writeTag(Tag tag){
    try
    {
    nfcsettings();
    Locale locale = Locale.ROOT;
    final byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
    final byte[] textBytes = etUser.toString().getBytes(Charsets.UTF_8);
    final int utfBit = 0;
    final char status = (char)(utfBit + langBytes.length);
    final byte[] data = Bytes.concat(new byte[] {(byte) status}, langBytes, textBytes);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);

        NdefRecord[] records = {record};
        NdefMessage message = new NdefMessage(records);
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            ndef.writeNdefMessage(message);
        } else {
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                format.connect();
                format.format(message);
            }           
        }
    }
    catch (Exception e)
    {
        //Display toast when there is a write error
        Toast.makeText(getBaseContext(), "Tag write unsuccessful.", Toast.LENGTH_SHORT).show();
    }
}
private void nfcsettings(){
    if(!NfcAdapter.getDefaultAdapter(this).isEnabled()){
        Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }
}
}

编辑:我发现我的问题在于以下几行:

import com.google.common.base.Charsets;
import com.google.common.primitives.Bytes;

上面提到的导入显然没有与应用程序的其他部分一起导出。我的构建路径中包含了Guava 9,但这并不能奏效。。。如何在我的应用程序中包括字符集和字节导入???

编辑:已解决!!

事实证明,将默认android sdk中的guavalib.jar文件添加到我项目的lib文件夹中就可以了。

事实证明,将默认android sdk中的guavalib.jar文件添加到我的项目的lib文件夹中就可以了。

最新更新