Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
来源:互联网 发布:数据库怎么加密 编辑:程序博客网 时间:2024/05/17 03:09
Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
在项目开发中测试时崩溃,错误如上
该错误的意思是线程中存在没有捕获到的异常。一般情况下捕获异常使用
try { } catch (Exception e) { }
但是在线程池中,线程池在执行任务时捕获了所有异常,这样一来线程中所有的异常都无法捕获到抛出的异常。
即 try catch 捕获不到异常了。
Java中有一个接口,UncaughtExceptionHandler 描述如下:
static interface Thread.UncaughtExceptionHandler // 当 Thread 因未捕获的异常而突然终止时,调用处理程序的接口。
Thread类中的一个方法
static voidsetDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) //设置当线程由于未捕获到异常而突然终止,并且没有为该线程定义其他处理程序时所调用的默认处理程序。
我们需要实现这样一个接口UncaughtExceptionHandler
,然后在程序的主线程中设置处理程序。
代码如下
import java.lang.Thread.UncaughtExceptionHandler;//implements UncaughtExceptionHandlerpublic class Other extends Activity implements UncaughtExceptionHandler {//必须实现接口uncaughtException@Overridepublic void uncaughtException(Thread arg0, Throwable arg1) { //在此处理异常, arg1即为捕获到的异常 Log.i("AAA", "uncaughtException " + arg1); }}
现在还无法捕获到线程中的异常,还需要调用一个方法
Thread.setDefaultUncaughtExceptionHandler(this);
在OnCreate方法中调用一下即可
@Overrideprotected void onCreate(Bundle savedInstanceState) { //在OnCreate方法中调用下面方法,然后再使用线程,就能在uncaughtException方法中捕获到异常 Thread.setDefaultUncaughtExceptionHandler(this);}
完整代码如下
//导入包 import java.lang.Thread.UncaughtExceptionHandler; //implements UncaughtExceptionHandler public class Other extends Activity implements UncaughtExceptionHandler { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.th); //在此调用下面方法,才能捕获到线程中的异常 Thread.setDefaultUncaughtExceptionHandler(this); //获取创建的 Button btn1 = (Button) findViewById(R.id.btn); //给btn1添加一个点击监听事件 btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //调用我自己的方法 Init(); } }); } public void Init() { //开启线程 new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { //此处的 try catch 是无法捕获到异常的 try { ////////////////////////////////////////// //需要在线程中调用的方法purchase.init(context, iapListener) purchase.init(context, iapListener); ////////////////////////////////////////// } catch (Exception e) { // TODO: handle exception Log.i("AAA", "INIG " + e); e.printStackTrace(); } } }); } //必须实现接口uncaughtException @Override public void uncaughtException(Thread arg0, Throwable arg1) { //在此处理异常, arg1即为捕获到的异常 Log.i("AAA", "uncaughtException " + arg1); }
这样再次运行就会捕获到线程中的异常,不会导致项目直接崩溃
2 0
- Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
- Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
- threadid=1: thread exiting with uncaught exception (group=0x416298c8)
- (Android)threadid = 1:thread exiting with uncaught exception(group = 0x40015560)
- Android中报错:threadid=1: thread exiting with uncaught exception
- 开发中关于threadid=1: thread exiting with uncaught exception (group=0x40...)的错误
- threadid=1: thread exiting with uncaught exception (group=0x40e40438)
- threadid=1: thread exiting with uncaught exception (group=0x40f11438)
- threadid=1: thread exiting with uncaught exception (group=0x416f2e00)
- W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41cb7c68)
- W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)
- android studio 运行报错 W/ dalvikvm threadid=1 thread exiting with uncaught exception group=0x41c57c50
- threadid=1: thread exiting with uncaught exception
- threadid=11: thread exiting with uncaught exception (group=0x41898c38)
- 问题的解决: dalvikvm(12291): threadid=1: thread exiting with uncaught exception (group=0x417d6930)
- 解决 threadid=1: thread exiting with uncaught exception (未测试)
- Android 应用Crash Log在Logcat中消失的问题 threadid=1: thread exiting with uncaught exception
- android问题:11-24 01:57:06.230: W/dalvikvm(1325): threadid=1: thread exiting with uncaught exception (
- F.NET框架示例(五)、数据库操作 之 修改
- 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()
- A*(A星)算法python实现
- <iOS>关于Xcode上的Other linker flags
- 移动办公——智能终端应用实例一二
- Android错误threadid=1: thread exiting with uncaught exception (group=0x416298c8)
- MF(二) Media Session
- Nginx:双向队列(Queue)
- github page 绑定域名
- java中Boolean和boolean的区别
- 一步步学习Hibernate框架(四):采用jpa实现一对多关联映射(二)
- git 批量删除分支
- git svn --ignore-paths 注意点
- 谈谈iOS Animation