我在我的 'sUncaughtExceptionHandler
中为当前线程设置了一个默认值,并从我的侦听器中抛出一个,但没有捕获到异常。我也尝试从and抛出异常,但没有被捕获。但是,当从或抛出时,它会被捕获。Activity
onCreate()
RunTimeException
AlertDialog
onClick()
onCreateOptionsMenu()
onOptionsItemSelected()
onCreate()
onResume()
这是我的活动:
public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(new DefExcHandler());
Log.d(TAG, "onCreate: "+Thread.currentThread().getName()+" "+Thread.currentThread().getId());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setNeutralButton("CRASH", this);
AlertDialog dialog = builder.create();
dialog.show();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
}
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Log.d(TAG, "onClick: "+Thread.currentThread().getName()+" "+Thread.currentThread().getId());
throw new RuntimeException();
}
}
这是我的异常处理程序:
public class DefExcHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "DefExcHandler";
public DefExcHandler() {
Log.d(TAG, "DefaultExceptionHandler: ");
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.d(TAG, "Uncaught exception caught: "+ex.getMessage()+" "+ex.getCause().toString());
}
}
根据我在活动中制作的日志消息,线程在onCreate()
和中是相同的onClick()
:
09-27 15:00:57.260 10667-10667/a.b.c D/MainActivity: onCreate: main 1
09-27 15:01:52.680 10667-10667/a.b.c D/MainActivity: onClick: main 1
为什么从onClick()
,onCreateOptionsMenu()
或抛出异常时没有捕获到异常onOptionsItemSelected()
?