Toast.makeText 方法出错 java.lang.RuntimeException
接手以前同事留下的代码,今天突然出现了一个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的更多相关文章
- android Toast提示异常:java.lang.RuntimeException: Can't create handler inside thread that has not called
Toast只能在UI线程弹出,解决此问题可以在Toast前后加两行代码,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(),& ...
- 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 ...
- resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind
开启resin 服务器以后提示如下:(控制台不断的循环循环打印如下错误提示) java.lang.RuntimeException: java.net.SocketException: Unrecog ...
- MapReduce错误之Error: java.lang.RuntimeException: java.lang.NoSuchMethodException的解决方法
今天跑MapReduce项目的时候遇到了这个问题,日志如下所示: // :: DEBUG ipc.ProtobufRpcEngine: Call: getDiagnostics took 19ms E ...
- 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 ...
- java.lang.RuntimeException: Unable to start activity ComponentInfo
异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.Result ...
- struts2 错误:Dispatcher initialization failed java.lang.RuntimeException
严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTargetE ...
- 转载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 ...
- 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': ...
随机推荐
- 注入AspectJ切面
为什么要用AspectJ:AspectJ提供了Spring AOP很多不能实现的多种切点类型(比如属性,构造方法切入,由于不能实现构造方法的切入spring aop就不能实现对象创建过程的通知) As ...
- cloudstack ssvm 管理地址不够造成无法启动修复过程
cloudstack日志记录: 上面已经提示了,管理ip没有了,造成这个原因很多,遇到过ssvm非正常关闭就有可能不释放IP慢慢把IP消耗掉.总之这肯定是BUG.按照上面的提示找到对应pod 和dc ...
- 「Luogu4321」随机游走
「Luogu4321」随机游走 题目描述 有一张 \(n\) 个点 \(m\) 条边的无向图,\(Q\) 组询问,每次询问给出一个出发点和一个点集 \(S\) ,求从出发点出发随机游走走遍这个点集的期 ...
- 51nod1031 骨牌覆盖 组合数学
不难发现,只有$1 * 2, 2 * 2$两种方法 因此,设$f[i]$表示填满$1 - i$的方案数 那么有$f[i] = f[i - 1] + f[i - 2]$,其实就是斐波那契数列.... 复 ...
- The 2016 ACMICPC Asia Beijing Regional Contest
A. Harmonic Matrix Counter (3/19) B. Binary Tree (1/14) C. Asa's Chess Problem (21/65) [ Problem ] 给 ...
- 树莓派(Debian)系统开启iptables的raw表实现日志输出
说明:可能Debian默认不开启iptables的raw表,所以无法通过其实现日志跟踪. 日志跟踪:http://www.cnblogs.com/EasonJim/p/8413563.html 解决方 ...
- MySQL时间戳与日期互转
1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() ); 输出:2006-08-22 12:11:10 2.日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() sel ...
- LWIP轻量级TCPIP协议栈的移植
http://blog.csdn.net/ygrx/article/details/8020516 好久没有做过技术工作了,前几天因为一些需要,要在ST的OS20平台上进行了LWIP的移植,有一些心得 ...
- 如何将Emmet安装到Notepad++?
1.下载Notepad++插件;(zen-Coding for Notepad++)2.解压后将压缩包中的文件放入Notepad++安装目录下的plugins文件夹中;3.重新启动就能看到菜单栏上增加 ...
- UIButton 设置圆角 边框颜色 点击回调方法
UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; signBtn.frame = CGRectMake(, , , ) ...