0

我正在开发一个基于 NFC 的 android 应用程序。使用 Handler 类发送带有 Message 的对象时,出现 arrayIndexOutOfBoundsException length = 0 index = 0 错误。

Process: com.telpo.tps530_demo, PID: 8949
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at com.telpo.tps530_demo.NFCDesfire$MyHandler.handleMessage(NFCDesfire.java:169)
    

处理程序类

 private class MyHandler extends Handler{
    private WeakReference<NFCDesfire> activityWeakReference;
    public MyHandler(NFCDesfire activity){
        activityWeakReference = new WeakReference<>(activity);
    }

覆盖

@Override
    public void handleMessage(Message msg) {
        NFCDesfire activity = activityWeakReference.get();
        Context context = activity.getApplicationContext();
        if(activity!=null) {
            switch (msg.what) {
                case CHECK_NFC_TIMEOUT: {
                    Toast.makeText(context, "TIME OUT !", Toast.LENGTH_LONG).show();
                    activity.nfc_open_btn.setVisibility(4);
                    activity.nfc_close_btn.setVisibility(4);
                    activity.nfc_check_btn.setVisibility(4);
                }
                break;
                case SHOW_NFC_DATA: {
                   byte[] uid_data = (byte[]) msg.obj;
                    if (uid_data[0] == 0x41) {
                        Toast.makeText(NFCDesfire.this,"Başarılı",Toast.LENGTH_SHORT).show();

                        byte[] atqa = new byte[2];
                        byte[] sak = new byte[1];
                        byte[] uid = new byte[uid_data[5]];

                        System.arraycopy(uid_data, 2, atqa, 0, 2);
                        System.arraycopy(uid_data, 4, sak, 0, 1);
                        System.arraycopy(uid_data, 6, uid, 0, uid_data[5]);

                        Log.e("yw", " " + atqa[0] + "  " + atqa[1] + "  " + sak[0]);
                        islemBitti = false;

错误线

uid_data[0] == 0x41

我应该在这里做什么?有人有解决方案吗?有代码的延续,所以如果需要,我可以分享源代码。

4

1 回答 1

0

在进行任何处理之前,您必须检查 msg.obj 的长度;似乎 msg.obj 是空的,并且您正在尝试访问它的第一个元素,因此它会引发 ArrayIndexOutOfBoundsException。

于 2021-02-17T12:26:40.757 回答