android设置缺省的异常捕获器

来源:互联网 发布:印度人是黄种人吗 知乎 编辑:程序博客网 时间:2024/05/01 09:54

import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Button tv = (Button)this.findViewById(R.id.btn_make_crash);tv.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        Log.i("xxx", "make null pointer exception");        String str = null;        str.length();    }});    Button reg = (Button)this.findViewById(R.id.btn_reg);    reg.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            Log.i("jklxxx", "reg ");            Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(                      MainActivity.this.getApplicationContext()));                    }});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;}private class DefaultExceptionHandler implements UncaughtExceptionHandler {      private Context act = null;      public DefaultExceptionHandler(Context act) {         this.act = act;      }      @Override      public void uncaughtException(Thread thread, Throwable ex) {          Log.i("jklxxx", "catch exception");        //ex.printStackTrace();        //android.os.Process.killProcess(android.os.Process.myPid());        //System.exit(0);    }  }  

}

0 0
原创粉丝点击