英文:
UncaughtExceptionHandler can't start activities
问题
以下是您要翻译的代码部分:
// Application.java
public final class Application extends android.app.Application {
public Application() {
Thread.setDefaultUncaughtExceptionHandler(new Application.UncaughtExceptionHandler());
}
static Context context;
static final class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
context.startActivity(new Intent()
.setClass(context, RuntimeException.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
/*.putExtra("u", true).putExtra("e", e)*/);
//System.exit(0);
}
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
// RuntimeException.java
public class RuntimeException extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_exception);
Bundle extras = getIntent().getExtras();
boolean uncaught = extras.getBoolean("u", false);
Throwable exception = (Throwable) extras.get("e");
String exceptionType;
if (uncaught) exceptionType = "Uncaught exception:\n"; else exceptionType = "Exception:\n";
TextView msg = findViewById(R.id.ex_msg);
msg.setText(exceptionType + exception.getLocalizedMessage());
}
}
<!-- AndroidManifest.xml -->
<application
android:name=".application.Application"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyTheme"
tools:targetApi="31">
<activity
android:name=".activity.Launcher"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.FileList"/>
<activity android:name=".activity.RuntimeException"/>
</application>
请注意,我已经将HTML实体字符(如"
)替换为正常的引号字符。
英文:
I can't find a solution to this problem. I have created an Application class, where I am setting the default uncaught exception handler, and I want to start an activity from there when an exception occurrs:
public final class Application extends android.app.Application {
public Application() {
Thread.setDefaultUncaughtExceptionHandler(new Application.UncaughtExceptionHandler());
}
static Context context;
static final class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
context.startActivity(new Intent()
.setClass(context, RuntimeException.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
/*.putExtra("u", true).putExtra("e", e)*/);
//System.exit(0);
}
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
This is the RuntimeException class, it is also defined in the manifest:
public class RuntimeException extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_exception);
Bundle extras = getIntent().getExtras();
boolean uncaught = extras.getBoolean("u", false);
Throwable exception = (Throwable) extras.get("e");
String exceptionType;
if (uncaught) exceptionType = "Uncaught exception:\n"; else exceptionType = "Exception:\n";
TextView msg = findViewById(R.id.ex_msg);
msg.setText(exceptionType + exception.getLocalizedMessage());
}
}
This is the manifest:
<application
android:name=".application.Application"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyTheme"
tools:targetApi="31">
<activity
android:name=".activity.Launcher"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.FileList"/>
<activity android:name=".activity.RuntimeException"/>
</application>
答案1
得分: 1
-
您需要调用Intent的setClass方法,并传递有效的Activity类,才能实际启动一个Activity,而不是Exception.class。
-
当主线程发生未捕获的异常时,主事件循环会终止,因此当主线程的异常处理程序返回时,主线程退出并终止程序。
-
详细信息:当Android主线程发生未捕获的异常时,主线程的未捕获异常处理程序会在主线程的事件循环退出后被调用。这意味着主线程最终会返回,而操作系统将终止该进程。
英文:
-
You need to call setClass on Intent with valid Activity class to actually start an Activity not Exception.class
-
when main thread's uncaught exception occurs, the main event loop is terminated, so when the main thread's exception handler returns, main thread exit and program terminates.
detail of 2.
android main Thread uncaught exception handler is called after the main thread's event loop exit. Which means that main Thread eventually return, and OS will kill the process.
答案2
得分: 1
在调用 `startActivity` 后应该杀死进程
static final class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
context.startActivity(new Intent()
.setClass(context, RuntimeException.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
/*.putExtra("u", true).putExtra("e", e)*/);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
英文:
Should kill process after calling startActivity
static final class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
context.startActivity(new Intent()
.setClass(context, RuntimeException.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
/*.putExtra("u", true).putExtra("e", e)*/);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论