1. 程式人生 > >子執行緒Handler sendMessage 報錯 Attempt to invoke virtual method '

子執行緒Handler sendMessage 報錯 Attempt to invoke virtual method '

                03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: FATAL EXCEPTION: main03-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: Process: tech.androidstudio.tulingdemo, PID: 1602903-17 08:10:18.760 16029-16029/tech.androidstudio.tulingdemo E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method for android:onClick03-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.InvocationTargetException03-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 reference03-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;    @Overrideprotected 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(){            @Overridepublic 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 referenceThread 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),因為 子執行緒不能修改介面的UIMessage message = Message.obtain();            message.what=SUB_HANDLER_ADD;            message.obj=message_send;            subHandler.sendMessage(message);//之前報錯的位置是這一步        }    }    @Overridepublic void run() {        //繫結 當前執行緒的LooperLooper.prepare();            subHandler= new Handler(){                @Overridepublic 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裡面停止這個LooperLooper looper = Looper.myLooper();                            looper.quit();                            break;                    }                }            };        //不停的迴圈 ,就像是while一樣。Looper.loop();    }}