zoukankan      html  css  js  c++  java
  • 1安卓笔记 异常捕捉

    在程序崩溃的时候 ,捕捉异常

    步骤  首先在 activity的oncreat中调用  Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler( this.getApplicationContext(),crashInterface)); 
            实现一个继承UncaughtExceptionHandler的子类

     1 public class DefaultExceptionHandler implements UncaughtExceptionHandler {   
     2 
     3    private Context act = null;  
     4    crashInterface crashInterface;
     5    @Override
     6    public void uncaughtException(Thread arg0, Throwable arg1) {
          Intent sIntent=new Intent(act , CrashActivity.class);
    27        sIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//必须带上
    28        act.startActivity(sIntent);
    29 //       crashInterface.finsh();
    30  //此处异常
    31        return;
    32    }
    33    
    34    public DefaultExceptionHandler(Context act,crashInterface  crashInterface) {  
    35         this.crashInterface=crashInterface;
    36        this.act = act;  
    37     }  
    
    47 }

    在需要捕获异常的activity 中oncreat

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            view= getLayoutInflater().inflate(R.layout.activity_main, null);  
            
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            setContentView(view);
        
            crashInterface  crashInterface=new crashInterface() {
                
                @Override
                public void finsh() {
                    // TODO Auto-generated method stub
                    finish();
                    Process.killProcess(Process.myPid());
                 
                }
            };
            Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler( this.getApplicationContext(),crashInterface)); 
        }
  • 相关阅读:
    cf 1179 C
    P5055 【模板】可持久化文艺平衡树 可持久化fhqtreap
    bzoj4605: 崂山白花蛇草水 权值线段树套KDtree
    luoguP4173 残缺的字符串 FFT
    [HNOI2016]序列 CDQ+DP
    [TJOI2017]不勤劳的图书管理员
    loj2058 「TJOI / HEOI2016」求和 NTT
    bzoj4503: 两个串
    luoguP4721 【模板】分治 FFT
    [Cqoi2016]K远点对 K-Dtree
  • 原文地址:https://www.cnblogs.com/zey23/p/4503362.html
Copyright © 2011-2022 走看看