【问题标题】:Getting a "...must implement abstract method NfcAdapter.OnNdefPushCompleteCallback error获取“...必须实现抽象方法 NfcAdapter.OnNdefPushCompleteCallback 错误
【发布时间】:2013-10-05 18:01:08
【问题描述】:

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

在我的班级 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();
    }
}

【问题讨论】:

    标签: android nfc ndef nfc-p2p android-beam


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你,我正试图找出问题所在
    猜你喜欢
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2013-06-12
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多