在我的 Android 应用程序中,我需要管理自定义 Activity 类中所有未捕获的异常。现在我在这里遇到问题,我无法从内部启动 Activity。即使 Activity 没有开始,也没有例外。
public class MyActivity extends Activity {
Context context;
Throwable throwable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=getApplicationContext();
final AlertDialog.Builder builder= new AlertDialog.Builder(this);
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
throwable=ex;
new Thread(){
@Override
public void run() {
Looper.prepare();
builder.setTitle("Warning...!");
builder.create();
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setPositiveButton("More Details", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent= new Intent(MyActivity.this,Report.class);
intent.putExtra("error", throwable);
startActivity(intent);
}
});
builder.setMessage(throwable.getMessage());
builder.show();
Looper.loop();
}
}.start();
}
});
}
}