子线程Handler.sendMessage 报错:Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage

这篇博客探讨了在Android开发中遇到的子线程Handler.sendMessage报错问题,错误日志显示在主线程中调用了子线程的Handler。原因是未在子线程启动前正确初始化Handler。解决方法是在onCreate方法内启动Thread,并确保传递正确的this参数以避免后续错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: FATAL EXCEPTION: main
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: Process: tech.androidstudio.tulingdemo, PID: 16029
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method for android:onClick
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View.performClick(View.java:4761)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19767)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View.performClick(View.java:4761) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19767) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at tech.androidstudio.tulingdemo.MainActivity.btnSendMessage(MainActivity.java:65)
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View.performClick(View.java:4761) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19767) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 

03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 


原因分析 :

 子线程handler 报错,是因为没有启动子线程就开始使用了。


解决办法:

在onCreate方法中启动Thread,千万别忘了Thread里面的this 参数,不然还会报错。

Thread thread = new Thread(this);
        thread.start();


public class MainActivity extends AppCompatActivity implements Runnable {
private EditText mEditText;
    private ListView mListView;
    private List<String> mData;
    private ListViewAdapter mAdapter;
    private Handler subHandler;
    private Handler mainHandler;
    private final int MAIN_HANDLER_ADD=98;
    private final int SUB_HANDLER_ADD=998;
    private final int SUB_HANDLER_EXIT=199;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText=(EditText)findViewById(R.id.editText);
        mListView=(ListView)findViewById(R.id.listView);
        mData = new ArrayList<>();
        mAdapter = new ListViewAdapter(mData,this);
        mListView.setAdapter(mAdapter);
        mainHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int what = msg.what;
                switch (what){
                    case MAIN_HANDLER_ADD:
                        String msg_reply = (String)msg.obj;
                        mData.add(msg_reply);
                        mAdapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        //千万别忘了启动子线程,不然会报错
        //Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage(android.os.Message)' on a null object reference
        Thread thread = new Thread(this);
        thread.start();
    }



    public void btnSendMessage(View view) {
        String message_send = mEditText.getText().toString();
        if(message_send!=null){
            mData.add(message_send);
            mAdapter.notifyDataSetChanged();
            //将要发送的消息发送给子线程,(给子线程的handler),因为耗时操作不能在主线程进行
            // 然后子线程请求数据再返回给主线程(主线程的 handler),因为 子线程不能修改界面的UI
            Message message = Message.obtain();
            message.what=SUB_HANDLER_ADD;
            message.obj=message_send;
            subHandler.sendMessage(message);//之前报错的位置是这一步
        }
    }

    @Override
    public void run() {
        //绑定 当前线程的Looper
        Looper.prepare();
            subHandler= new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    int what = msg.what;
                    switch (what){
                        case SUB_HANDLER_ADD:
                            //获取发送的消息,然后通过HttpUtils来发送,并且得到返回值,
                            //把这个返回值 再给主线程的 handler来处理。
                            //先写一个模拟数据 ,例如"收到了"发送给主线程。
                            Message message = Message.obtain();
                            message.what=MAIN_HANDLER_ADD;
                            message.obj="收到了";
                            mainHandler.sendMessage(message);
                            break;
                        case SUB_HANDLER_EXIT:
                            //onDestroy里面停止这个Looper
                            Looper looper = Looper.myLooper();
                            looper.quit();
                            break;
                    }
                }
            };
        //不停的循环 ,就像是while一样。
        Looper.loop();
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值