0

我正在尝试使用http://www.tappednfc.com/wp-content/uploads/TAPPED-NFCDeveloperGuide-Part1.pdf上的文档来学习 NFC 。

在我的类 id 定义的地方,我收到以下错误:必须实现抽象方法NfcAdapter.OnNdefPushCompleteCallback.onNdefPushComplete

我定义了以下方法

public void OnNdefPushComplete( NfcEvent arg0)
{
    mHandler.obtainMessage(1).sendToTarget();
}

这不是错误消息说我需要的回调吗?

完整代码如下

public class BeamActivity extends Activity
                          implements CreateNdefMessageCallback,
                                     OnNdefPushCompleteCallback {
    NfcAdapter mNfcAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // see if there is a NFC interface
        mNfcAdapter=NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter==null) Toast.makeText(this,"no adapter",
                                              Toast.LENGTH_SHORT).show();

        mNfcAdapter.setNdefPushMessageCallback(this,this);
        mNfcAdapter.setOnNdefPushCompleteCallback(this,this);
    }

    public void OnNdefPushComplete( NfcEvent arg0) {
        mHandler.obtainMessage(1).sendToTarget();
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(),"Mesg Sent",
                                   Toast.LENGTH_SHORT).show();
                    break;
            } // end switch
        } // end handle mesg
    }; // end new

    // create call back code
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text="hello world";
        //NdefMessage msg = new NdefMessage(new NdefRecord[] {
        //        NfcUtils.
        //    }
        return msg;
    }

    @Override
    public void onNewIntent(Intent intent) {
        setIntent(intent);
    }

    @Override
    public void onResume() {
        super.onRestart();

        if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent( getIntent() );
        }
    }

    void processIntent( Intent intet) {
        Parcelable[] rawMsgs= intet.getParcelableArrayExtra(
                mNfcAdapter.EXTRA_NDEF_MESSAGES );

        NdefMessage msg = (  NdefMessage) rawMsgs[0];
        String s= new String(  msg.getRecords()[0].getPayload());

        Toast.makeText(getApplicationContext(), s,
                       Toast.LENGTH_SHORT).show();
    }
}
4

1 回答 1

1

请记住,Java 区分大小写。该方法OnNdefPushCompleteCallback.onNdefPushComplete(...)以小写的“o”开头(参见接口定义OnNdefPushCompleteCallback

public void onNdefPushComplete(NfcEvent arg0) {
    mHandler.obtainMessage(1).sendToTarget();
}
于 2013-10-05T19:38:06.913 回答