--------------android开发中的crash------问题--------------------

开发中陆陆续续遇到很多莫名其妙的问题,其中最让人头疼的就是application crash。application的crash可能由于很多的原因所引起的,有可能是因为数据的问题,或者是异步AsyncTask的运用不合理,等等等等,都可能造成application crash。

也一直在想如何才能知道application crash,想在application crash的时候做点什么,一直也没有找到一个solution。后来在一个偶然的机会用到一个别人的application可以捕获到crash,就想捡到了宝一样。想尽办法去找到这个application的sourcecode,发现原来很简单,几句话就可以做到。

 

1. 在Activity的onCreate方法里面添加这句话:

Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this));

 

[java]  view plain copy
  1. @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.           
  5.         Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));  
  6.           
  7.         setContentView(R.layout.main);  
  8.           
  9.         click2Crash = (Button)findViewById(R.id.clickToCrash);  
  10.           
  11.         click2Crash.setOnClickListener(new Button.OnClickListener(){  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.                 String text = "aaa";  
  15.                 System.out.print(text.charAt(10));  
  16.             }});  
  17.     }  
 

 

2. 写一个自己的MyUncaughtExceptionHandler implementes UncaughtExceptionHandler,实现方法uncaughtException。在这个方法里面做任何想做的事情,比如说将造成application crash的原因发送给开发人员。 

[java]  view plain copy
  1. public class UncaughtExceptionHandler implements  
  2.         java.lang.Thread.UncaughtExceptionHandler {  
  3.     private Context con_;  
  4.     public UncaughtExceptionHandler(Context con){  
  5.         this.con_ = con;  
  6.     }  
  7.     @Override  
  8.     public void uncaughtException(Thread thread, Throwable ex) {  
  9.         StringWriter sw = new StringWriter();  
  10.         ex.printStackTrace(new PrintWriter(sw));  
  11.         System.err.println(sw);  
  12.         Intent bugReportIntent = new Intent(con_, BugReportActivity.class);  
  13.         bugReportIntent.putExtra(BugReportActivity.exceptionMsg, sw.toString());  
  14.         con_.startActivity(bugReportIntent);  
  15.           
  16.         Process.killProcess(Process.myPid());  
  17.         System.exit(10);  
  18.     }  
  19. }  
 

3. 写一个自己的Activity,告诉用户,你需要这个exception信息,让他Email给开发人员。

[java]  view plain copy
  1. public class BugReportActivity extends Activity {  
  2.     public static final String exceptionMsg = "exceptionMsg";  
  3.       
  4.     private TextView reportContent;  
  5.     private Button sendMailBtn;  
  6.     private Button cancelBtn;  
  7.       
  8.     protected void onCreate(Bundle bundle){  
  9.         super.onCreate(bundle);  
  10.         Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));  
  11.           
  12.         setContentView(R.layout.bug_report);  
  13.           
  14.         reportContent = (TextView)findViewById(R.id.reportContent);  
  15.         sendMailBtn = (Button)findViewById(R.id.sendMail);  
  16.         cancelBtn = (Button)findViewById(R.id.cancel);  
  17.           
  18.         String sw = getIntent().getStringExtra(exceptionMsg);  
  19.         reportContent.setText(sw);  
  20.           
  21.         initHandler();  
  22.     }  
  23.       
  24.     private void initHandler(){  
  25.         sendMailBtn.setOnClickListener(new Button.OnClickListener(){  
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 Toast.makeText(v.getContext(), "Email will be sent to our helpdesk.", Toast.LENGTH_LONG).show();  
  29.             }  
  30.         });  
  31.         cancelBtn.setOnClickListener(new Button.OnClickListener(){  
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 finish();  
  35.             }  
  36.         });  
  37.     }  
  38. }  
 

更不错的可以看下2014版的vlc中的关于crash的处理, 用了StackTraceElement[] trace = ex.getStackTrace(); 追踪.

 https://github.com/ACRA/acra

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值