代碼改變世界
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();
} };
完美解決!!!
