java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解決辦法


代碼改變世界

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解決辦法

 

android Toast提示異常:java.lang.RuntimeException: Can't create handler inside thread that has not called

原因是在子線程彈Toast了, 切記,Toast只能在UI線程彈出

 

解決辦法:

new Thread(new Runnable() {  
              
            @Override  
            public void run() {  
                // TODO Auto-generated method stub  
                  
                try {  
                    Thread.sleep(1000);  
                    handler.sendEmptyMessage(0);  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                  
            }  
        }).start();

  

private Handler handler = new Handler() {  
  
        @Override  
        public void handleMessage(Message msg) {
      Toast.makeText(CommodityDetails.this, "修改失敗!", Toast.LENGTH_SHORT).show();
} };

  

完美解決!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM