W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)
今天编程时候遇到一个问题:
Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
在项目开发中测试时崩溃,错误如上
该错误的意思是线程中存在没有捕获到的异常。一般情况下捕获异常使用
try {} catch (Exception e){}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
但是在线程池中,线程池在执行任务时捕获了所有异常,这样一来线程中所有的异常都无法捕获到抛出的异常。
即 try catch 捕获不到异常了。
Java中有一个接口,UncaughtExceptionHandler 描述如下:
static interface Thread.UncaughtExceptionHandler
// 当 Thread 因未捕获的异常而突然终止时,调用处理程序的接口。
- 1
- 2
- 1
- 2
Thread类中的一个方法
static void
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
//设置当线程由于未捕获到异常而突然终止,并且没有为该线程定义其他处理程序时所调用的默认处理程序。
- 1
- 2
- 3
- 1
- 2
- 3
我们需要实现这样一个接口UncaughtExceptionHandler
,然后在程序的主线程中设置处理程序。
代码如下
import java.lang.Thread.UncaughtExceptionHandler;//implements UncaughtExceptionHandler
public class Other extends Activity implements UncaughtExceptionHandler {//必须实现接口uncaughtException
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {//在此处理异常, arg1即为捕获到的异常 Log.i("AAA", "uncaughtException " + arg1);}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
现在还无法捕获到线程中的异常,还需要调用一个方法
Thread.setDefaultUncaughtExceptionHandler(this);
- 1
- 1
在OnCreate方法中调用一下即可
@Override
protected void onCreate(Bundle savedInstanceState) {//在OnCreate方法中调用下面方法,然后再使用线程,就能在uncaughtException方法中捕获到异常Thread.setDefaultUncaughtExceptionHandler(this);
}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
完整代码如下
//导入包import java.lang.Thread.UncaughtExceptionHandler;//implements UncaughtExceptionHandlerpublic class Other extends Activity implements UncaughtExceptionHandler {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.th);//在此调用下面方法,才能捕获到线程中的异常Thread.setDefaultUncaughtExceptionHandler(this);//获取创建的 Buttonbtn1 = (Button) findViewById(R.id.btn);//给btn1添加一个点击监听事件btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {//调用我自己的方法Init();}});}public void Init() {//开启线程new Handler(Looper.getMainLooper()).post(new Runnable() { @Overridepublic void run() {//此处的 try catch 是无法捕获到异常的try {////////////////////////////////////////////需要在线程中调用的方法purchase.init(context, iapListener)purchase.init(context, iapListener);//////////////////////////////////////////} catch (Exception e) {// TODO: handle exceptionLog.i("AAA", "INIG " + e);e.printStackTrace();}}});}//必须实现接口uncaughtException@Overridepublic void uncaughtException(Thread arg0, Throwable arg1) {//在此处理异常, arg1即为捕获到的异常Log.i("AAA", "uncaughtException " + arg1);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
这样再次运行就会捕获到线程中的异常,不会导致项目直接崩溃
android.view.ViewGroup.LayoutParams
改为:
android.widget.AbsListView.LayoutParams
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)相关推荐
- 【elasticsearch】ES启动报错 uncaught exception in thread [main]org.elasticsearch.bootstrap.Startup
完整的错误信息是: uncaught exception in thread [main]org.elasticsearch.bootstrap.StartupException: java.la ...
- 【22.05.14】native thread exiting without having called DetachCurrentThread
native thread exiting without having called DetachCurrentThread: javaVM->AttachCurrentThread之后要在某 ...
- jmeter使用报错ERROR o.a.j.JMeter: Uncaught exception in thread Thread[AWT-EventQueue-0,6,main]
最近开始自学接触jmeter,但是在跑接口的时候发现报错了(接口使用postman已经跑通过,在自查一通以后终于发现原因) 先上错误提示:我们主要先看error,可以看到ERROR o.a.j.JMe ...
- JMeter直连数据库报错Uncaught Exception java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
报错:Uncaught Exception java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z in thread ...
- Terminating app due to uncaught exception 'NSGenericException' 类崩溃文章收集
返回崩溃:nested pop animation can result in corrupted navigation bar nested pop animation can re 使用Xcode ...
- terminating with uncaught exception of type std::bad_cast: std::bad_cast
terminating with uncaught exception of type std::bad_cast: std::bad_cast 我这里的情况: 我有两个so A 和B,调用程序c 把 ...
- BB之Uncaught exception:net.rim.device.api....错误的解决之道
最近弄了包月GPRS,于是开始捣鼓bb的Gmail功能...可是, 装了N遍的Gmail 1.5 或2.0 版本的程序后, 打开, 界面总是会出现 "Uncaught exception:n ...
- 使用imagick将PDF转换成图片时报Fatal error: Uncaught exception 'ImagickException' with message 'FailedToExecute
个人博客原文地址:http://www.lampnick.com/php/720 $IM = new imagick(); $IM->setResolution(200, 200); $IM-& ...
- ios 常见异常之- Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason:
异常全部信息: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<S ...
最新文章
- 程序员效率低下的35个坏习惯
- python批量分析表格_Python环境下百度Ocr表格批量识别
- VB6 XArrayDB | Xarray ReDim 用法
- [POJ 1003] Hangover C++解题
- pfSense book之 Open***站点到站点连接示例(共享密钥)
- powerbi add visual diagram
- C++线程池原理及创建(转)
- 现代软件工程系列 学生读后感 梦断代码 SpringGreen
- PHP操作视频音频类 FFmpegPHP
- 如果40岁仍碌碌无为,牢记2句话,最迟也是大器晚成
- bzoj 3027: [Ceoi2004]Sweet(母函数+组合数)
- .net反编译工具reflector5.0 的介绍及使用
- 有将视频横屏改为竖屏的方法吗?
- 公司要收我的毕业证书,这合法吗?——网上答疑(17)
- CEOI 2020, Day 2 A,B题解 CF1403A CF1403B
- python批量改文件名,截取原文件名的一部分
- 在linux下运用mutt和msmtp发邮件
- 数据中心的“灾备”指的是什么?
- 2023年最新前端面试题
- SQL*Plus 系统变量之28 - LOBOF[FSET]