子執行緒Handler.sendMessage 報錯:Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage
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(); } }
相關推薦
子執行緒Handler.sendMessage 報錯:Attempt to invoke virtual method 'boolean android.os.Handler.sendMessage
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.
Android報錯:Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)'
專案中遇到空指標異常 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Str
報錯Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)'
問題:Caused by: android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual met
子執行緒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.and
pymysql多執行緒訪問資料庫報錯:Packet sequence number wrong - got 7 expected 2
原文:https://www.cnblogs.com/heiao10duan/p/9373237.html 參考:https://www.jianshu.com/p/60c8e0e440ea 原因: 使用了多執行緒,多執行緒共享了同一個資料庫連線,但每個execute前沒有加上互斥鎖
appium 運行報錯:...... Attempt to re-install io.appium.settings without first uninstalling解決方案
pos failed ron autotest emp 報錯 tro sta ngs 報錯形式: Failed to install D:\AutoTest\appium\Appium\node_modules\appium\build\settings_apk\set
android 報錯Attempt to invoke virtual method
1.報錯資訊如下 java.lang.RuntimeException: Unable to start activity ComponentInfo{yixing.artshow/yixing.artshow.activity.SetInputActivity}: jav
android JPushInterface.setAlias 報錯 java.lang.NullPointerException: Attempt to invoke virtual method
報錯內容: 06-20 16:47:49.519: E/AndroidRuntime(21018): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zipingfang.wan
Android異常:java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.len
Android studio 異常:java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object refere
Android進階(十六)子執行緒呼叫Toast報Can't create handler inside thread that has not called Looper.prepare() 錯誤
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Android進階(十六)子執行緒呼叫Toast報Can't create handler inside thread that has not called Looper.prepare() 錯誤
原子執行緒呼叫Toast報Can't create handler inside thread that has not called Looper.prepare() 錯誤 今天用子執行緒調Toast報了一個Can't create handler inside thre
Linux執行source /etc/profile報錯“:command not found”
命令 csdn brush linux not etc highlight 可用 class 修改完 /etc/profile中的內容後,執行“立即生效”命令 “source /etc/profile”報錯: :command not found :command not
tf.app.run() 執行結束時,報錯:SystemExit exception: no description
環境:Python3.6.6 + tensorflow-gpu 原始碼如下: import tensorflow as tf def main(): print("hello tf.app.run") if __name__ == '__main__': tf.a
【坑】【maven】在PowerShell視窗下執行maven命令列報錯:Unknown lifecycle phase ".test.skip=true".
在PowerShell視窗下執行maven命令列報錯:Unknown lifecycle phase “.test.skip=true”. 不說廢話,先展示錯誤資訊 解決方法 方法一 命令改為:mvn clean install package '-Dmaven.test.skip=t
eclipse執行專案,tomcat報錯:Exception in thread :http-bio-8080-exec-4
原文地址:https://www.cnblogs.com/yby-blogs/p/6283105.html 今天用eclipse開發專案後,新建一個tomcat的server,然後新增專案後啟動,利用火狐瀏覽器進行訪問一直報記憶體溢位: Exception in threa
Windows64環境中執行pip install flask報錯:Cannot fetch index base URL http://pypi.python.prg/simple/
最近在學習Python的Flask框架,安裝flask是使用pip install flask命令,執行出錯:Cannot fetch index base URL http://pypi.python.prg/simple/ 看報錯資訊,以為是什麼配置檔案的引數U
小程式執行報錯:“Failed to load local image resource xxx.png the server responded with?
錯誤原因: 由於這是因為初始化的時候,變數還沒渲染進去導致的。 解決方法: 在src前面加個: wx:if<image wx:if='{{customerLevel.value}}' class
執行geth attach報錯:Unable to attach to remote geth: no known transport for URL scheme "c"
在window10上執行get attach命令啟動互動式JavaScript環境連線到節點的時候,報錯Unable to attach to remote geth: no known transport for URL scheme "c"。 但是在windows7或者geth1.8以前的版
啟動tomcat執行maven工程報錯:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:
控制檯報錯資訊: 解決方案:在maven工程pom.xml中新增如下依賴 <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId&g
WebStorm執行Node.js檔案報錯:runnerw.exe: CreateProcess failed with error 2
點選工具欄的 Run —> Edit Configurations… 可以看到我這裡是 Node interpreter 的路徑錯了 選擇一個正確的路徑即可 注意:也有可能是另一