首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义NFC应用程序不断崩溃

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

Stack Overflow用户
提问于 2012-09-12 11:23:57
回答 1查看 1.2K关注 0票数 1

我这里有一个非常具体的问题。我写了一个应用程序来发送消息给NFC芯片,但当我按下按钮时,它一直在崩溃。我使用的编辑器Eclipse不提供任何错误或警告,因此它不是代码中的错误。

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

应用程序标记写入器(process com.harold.tag.writer)意外停止。

你们能帮帮我吗?

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

代码语言:javascript
复制
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));
    }
}
}

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

代码语言:javascript
复制
import com.google.common.base.Charsets;
import com.google.common.primitives.Bytes;

上面提到的进口产品显然没有和应用程序的其他部分一起出口。我把番石榴9包括在我的建造道路上,但这不管用.如何将字符集和Bytes导入到我的应用程序中?

编辑:解决了!!

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-03 10:08:42

结果是,将默认的android中的guavalib.jar文件添加到我的项目的lib文件夹中就可以了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12387037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档