首页
论坛
课程
招聘
在pathclassloader和bootclassloader中间插入dexclassloader后运行报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@SuppressLint("SdCardPath")
public static void startActivitySecondMethod(Context context) {
    ClassLoader pathClassLoader = MainActivity.class.getClassLoader();
    assert pathClassLoader != null;
    ClassLoader bootClassLoader = pathClassLoader.getParent();
    dexClassLoader = new DexClassLoader("/sdcard/svip/my.dex", context.getApplicationContext().getCacheDir().getAbsolutePath(), null, bootClassLoader);
    try {
        @SuppressLint("DiscouragedPrivateApi")
        Field parent = ClassLoader.class.getDeclaredField("parent");
        parent.setAccessible(true);
        parent.set(pathClassLoader, dexClassLoader);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
 
    printClassLoader();
    try {
        Class<?> TestActivityClass = pathClassLoader.loadClass("dev.svip.mydex.TestActivity");
        context.startActivity(new Intent(context, TestActivityClass));
 
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

报错信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2022-05-14 00:17:20.916 26214-26214/dev.svip.test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: dev.svip.test, PID: 26214
    java.lang.RuntimeException: Unable to resume activity {dev.svip.test/dev.svip.test.MainActivity}: java.lang.IllegalStateException: Can't make a decor toolbar out of null
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4438)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7664)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.IllegalStateException: Can't make a decor toolbar out of null
        at androidx.appcompat.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:240)
        at androidx.appcompat.app.WindowDecorActionBar.init(WindowDecorActionBar.java:196)
        at androidx.appcompat.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:172)
        at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:557)
        at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:537)
        at androidx.appcompat.app.AppCompatDelegateImpl.onPostResume(AppCompatDelegateImpl.java:679)
        at androidx.appcompat.app.AppCompatActivity.onPostResume(AppCompatActivity.java:241)
        at android.app.Activity.performResume(Activity.java:8154)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4428)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7664)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

请问这是什么原因?谢谢。

收藏
0条回答