Handler Message中的java.lang.ClassCastException

时间:2016-09-01 08:56:06

标签: android handler

以下是错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demoapp.device.Dev
        at com.example.demoapp.MainActivity$MyHandler.handleMessage(MainActivity.java:177)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5140)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

这是我的处理程序代码错误发生在 new DevSetNameThread

class MyHandler extends Handler { 
    public MyHandler() {
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        new DevSetNameThread(((Dev) msg.obj), deviceNickName).start();
    }
}

以下是使用put对象

向send处理程序发送消息的代码
static final int CMD_TIMEOUT = 8;
static final int DISPLAY_DEVID = 9;
static final int FIND_DEVID = 3;
static final int FIND_ERROR = 4;
public static final int SCAN_CODE = 1;
static final int START_SUBMIT = 5;
static final int SUBMIT_ERROR = 2;
static final int SUBMIT_START = 0;
static final int SUBMIT_SUCCEED = 1;
public static final String TAG = "MainActivity";
static final int TIME_OUT = 6;

Handler handler;
List<Dev> listDev;
Msg msg;

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

    init();
}

private void init() {
    info = "";
    type = "";
    msg = new Msg();
    listDev = new ArrayList();
 }

 class SubmitThread extends Thread {
    SubmitThread() {
    }

    public void run() {
        if (listDev.size() > 0) {
            Dev d = (Dev) listDev.get(SUBMIT_START);
            listDev.remove(d);
            if (d != null) {
                MainActivity mainActivity;
                int index;
                Message message = new Message();
                message.what = SUBMIT_ERROR;
                Log.v("SubmitThread", "\u5f00\u59cb\u63d0\u4ea4\u6570\u636e start..");
                int delay = 50;
                int reSendTime = SUBMIT_SUCCEED;
                Cfg.isSubmitDev = false;
                String strId = d.getId();
                if (StrTools.stringToInt(strId.substring(SUBMIT_START, SUBMIT_SUCCEED)) == 0) {
                    mainActivity = MainActivity.this;
                    devId = strId.substring(SUBMIT_SUCCEED);
                } else {
                    devId = strId;
                }
                message.obj = devId;
                mainActivity = MainActivity.this;
                passwd = d.getPass();
                byte[] data = new byte[16];
                long val = StrTools.stringToInt(devId);
                int i = SUBMIT_START;
                int index2 = SUBMIT_START;
                while (i < CMD_TIMEOUT) {
                    index = index2 + SUBMIT_SUCCEED;
                    data[index2] = (byte) ((int) (val % 256));
                    val /= 256;
                    i += SUBMIT_SUCCEED;
                    index2 = index;
                }
                byte[] b = new byte[CMD_TIMEOUT];
                val = StrTools.stringToInt(passwd);
                for (i = SUBMIT_START; i < CMD_TIMEOUT; i += SUBMIT_SUCCEED) {
                    b[i] = (byte) ((int) (val % 256));
                    val /= 256;
                }
                byte[] buff = StrTools.byteToSwapByte(b);
                i = SUBMIT_START;
                while (i < CMD_TIMEOUT) {
                    index = index2 + SUBMIT_SUCCEED;
                    data[index2] = buff[i];
                    i += SUBMIT_SUCCEED;
                    index2 = index;
                }
                msg.setId(Cfg.userId);
                msg.setCmdType(MSGCMDTYPE.valueOf(-17));
                msg.setCmd(MSGCMD.valueOf((int) TIME_OUT));
                msg.setTorken(Cfg.tcpTorken);
                msg.setData(data);
                msg.setDataLen(data.length);
                protocol.MessageEnCode(msg);
                Log.i(TAG, "         data:" + StrTools.bytesToHexString(data));
                Log.i(TAG, "sendData:" + StrTools.bytesToHexString(msg.getSendData()));
                Message handlermsg = new Message();
                handlermsg.obj = devId;
                handlermsg.what = SUBMIT_START;
                handler.sendMessage(handlermsg);
                do {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    reSendTime--;
                    if (reSendTime <= 0) {
                        reSendTime = 10;
                        socketService.socketSendMessage(msg);
                    }
                    if (Cfg.isSubmitDev) {
                        message.what = SUBMIT_SUCCEED;
                        message.obj = d;
                        succedDev = d;
                        handler.sendMessage(message);
                        return;
                    }
                    delay--;
                } while (delay > 0);
                message.what = SUBMIT_ERROR;
                handler.sendMessage(message);
            }
        }
    }
}

很抱歉给我带来不便,我不能把整个代码。

我尝试使用标准代码转换检索到的对象,但它给了我这个错误,所以,我如何通过处理程序检索Dev对象并使用它?

0 个答案:

没有答案