MainActivity 的代码
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * 这个需要sd卡的读取权限 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView tv; Button btn; private boolean flag; private String tag; private int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** *这三句代码最好是写在activity的基类中这样才能捕获整个应用的异常 *不能写在MyApplication类里,因为对话框所需要的上下文要是activity的上下文 */ //获得UncaughtException捕获异常类 UncaughtException mUncaughtException = UncaughtException.getInstance(); //初始化,把当前对象设置成UncaughtExceptionHandler处理器 mUncaughtException.init(); //传递对话框所需要的上下文 mUncaughtException.setContext(this); flag=false; btn= (Button) findViewById(R.id.btn); tv= (TextView) findViewById(R.id.tv); btn.setOnClickListener(this); tv.setOnClickListener(this); } public void onClick(View view) { switch (view.getId()) { case R.id.tv: break; case R.id.btn: //抛出异常测试 String str=null; tv.setText(str.toString()); break; } } }