接手以前同事留下的代码,今天突然出现了一个bug:

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

而出错的代码就是一个Toast.makeText()方法。

这个方法很常见,但是报错的不多,前两天有过类似的经验,所以很快找到原因:Android中不允许其他线程更新主线程的视图。

修改很简单,创建一个handler,用来处理消息,然后把原来的处理代码放到handler中,调用的时候发送一个消息过去就可以了。

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == -1){
Toast.makeText(SetActivity.this, "无法连接网络,登录失败", Toast.LENGTH_LONG).show();
}
}
};
Message msg = new Message();
msg.what = -1;
handler.sendMessage(msg);

Toast.makeText 方法出错 java.lang.RuntimeException的更多相关文章

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

    Toast只能在UI线程弹出,解决此问题可以在Toast前后加两行代码,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(),& ...

  2. 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.pre ...

  3. resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind

    开启resin 服务器以后提示如下:(控制台不断的循环循环打印如下错误提示) java.lang.RuntimeException: java.net.SocketException: Unrecog ...

  4. MapReduce错误之Error: java.lang.RuntimeException: java.lang.NoSuchMethodException的解决方法

    今天跑MapReduce项目的时候遇到了这个问题,日志如下所示: // :: DEBUG ipc.ProtobufRpcEngine: Call: getDiagnostics took 19ms E ...

  5. java.lang.RuntimeException: java.lang.ClassNotFoundException: cmd.CmdWordCount$MyMapper解决方法

    14/02/28 20:29:48 INFO mapred.JobClient: Task Id : attempt_201402281833_0004_m_000000_1, Status : FA ...

  6. java.lang.RuntimeException: Unable to start activity ComponentInfo

    异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.Result ...

  7. struts2 错误:Dispatcher initialization failed java.lang.RuntimeException

    严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTargetE ...

  8. 转载java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinner/com.example.spinner.MainActivity}: java.lang.NullPointerException

    今天学习Android开发突然遇到了这个问题,查阅了很多资料,并且对集中原因进行了分析. 错误信息字符串:java.lang.RuntimeException: Unable to start act ...

  9. nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoSupport': ...

随机推荐

  1. ubuntu16.04.2安装完后重启报错[sda] Assuming drive cache: write through

    原因:检测主机的物理连接线,发生问题时"已连接"未勾选,重启的时候找不到iso文件 解决办法:勾选"已连接",重启机器成功

  2. RxJava 机制

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha rxjava 是  以 响应式 编程思想   编程的 java类库

  3. [BZOJ4554][TJOI2016&&HEOI2016]游戏(匈牙利)

    4554: [Tjoi2016&Heoi2016]游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 857  Solved: 506[Sub ...

  4. [ZJOI2007]最大半连通子图

    [ZJOI2007]最大半连通子图 题目大意: 一个有向图称为半连通的,当且仅当对于任意两点\(u,v\),都满足\(u\)能到达\(v\)或者\(v\)能到达\(u\). 给定一个\(n(n\le1 ...

  5. 腾讯通消息webSDK踩坑

    1.腾讯通提供一个通过http协议的接口,可用于发送消息,公告等功能,要使用其功能首先要开启RTX_HTTPServer服务. 2.阅读文档http://rtx.tencent.com/sdk/,为了 ...

  6. codeforces 85D D. Sum of Medians Vector的妙用

    D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/prob ...

  7. HF Reader

  8. Install Linux Kernel 4.10 In CentOS and Ubuntu

    https://www.ostechnix.com/install-linux-kernel-4-10-centos-ubuntu/

  9. Android Studio开发Android问题集【持续更新】

    问题一:emulator:ERROR:This AVD's configuration is missing a kernel file!! 答:打开Android SDK Manager,查看相应的 ...

  10. shell之if简化语句

    最常用的简化if语句: && 如果是“前面”,则“后面” [ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查 文 ...