UncaughtExceptionHandler 无法启动活动。

huangapple go评论69阅读模式
英文:

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实体字符(如&quot;)替换为正常的引号字符。

英文:

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(&quot;u&quot;, true).putExtra(&quot;e&quot;, 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(&quot;u&quot;, false);
		Throwable exception = (Throwable) extras.get(&quot;e&quot;);

		String exceptionType;
		if (uncaught) exceptionType = &quot;Uncaught exception:\n&quot;; else exceptionType = &quot;Exception:\n&quot;;

		TextView msg = findViewById(R.id.ex_msg);
		msg.setText(exceptionType + exception.getLocalizedMessage());
	}
}

This is the manifest:

&lt;application
        android:name=&quot;.application.Application&quot;
        android:allowBackup=&quot;true&quot;
        android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
        android:fullBackupContent=&quot;@xml/backup_rules&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/Theme.MyTheme&quot;
        tools:targetApi=&quot;31&quot;&gt;
        &lt;activity
            android:name=&quot;.activity.Launcher&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        &lt;activity android:name=&quot;.activity.FileList&quot;/&gt;
        &lt;activity android:name=&quot;.activity.RuntimeException&quot;/&gt;
    &lt;/application&gt;

答案1

得分: 1

  1. 您需要调用Intent的setClass方法,并传递有效的Activity类,才能实际启动一个Activity,而不是Exception.class。

  2. 当主线程发生未捕获的异常时,主事件循环会终止,因此当主线程的异常处理程序返回时,主线程退出并终止程序。

  3. 详细信息:当Android主线程发生未捕获的异常时,主线程的未捕获异常处理程序会在主线程的事件循环退出后被调用。这意味着主线程最终会返回,而操作系统将终止该进程。

英文:
  1. You need to call setClass on Intent with valid Activity class to actually start an Activity not Exception.class

  2. 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(&quot;u&quot;, true).putExtra(&quot;e&quot;, 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(&quot;u&quot;, true).putExtra(&quot;e&quot;, e)*/);
             android.os.Process.killProcess(android.os.Process.myPid());
             System.exit(0);
        }
    }

huangapple
  • 本文由 发表于 2023年7月18日 05:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定