我一直想知道是否可以通过捕获父活动中的崩溃来阻止Android应用程序崩溃.
假设我在子活动的onCreate方法中导致致命异常,我能否以任何方式捕获该异常?或者无论我尝试什么,应用程序都会崩溃?
这是我的意思的一个例子:
Main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ly_main);
// My Main activity starts
try{
// Call the next activity
Intent intent = new Intent(getApplicationContext(), Child.class);
startActivity(intent);
}catch(Exception e){
Log.wtf("Exception_WTF","Exception from child activity woohoo \n "+ e.toString());
}
Run Code Online (Sandbox Code Playgroud)
Child.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ly_child);
// Create exception... for science
int a = 0;
a = 1/a;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.子活动死亡并将父母带走.
是否可以通过startActivityForResult来实现?
谢谢,
编辑:我不需要崩溃数据,我只是想知道如何避免应用程序崩溃.
环顾四周我发现: 在android上使用全局异常处理
其中包括这件作品:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) …
Run Code Online (Sandbox Code Playgroud) android ×1