Android-система не может найти фрагмент во время восстановления активности, когда фрагмент загружен из библиотеки в SD-карту?

Я загрузил фрагмент из внешней библиотеки, доступной в SD-карте успешно, но когда система Android повторно воссоздает действие, при этом происходит сбой моего приложения, потому что система Android не может найти класс фрагмента, который доступен во внешней библиотеке.

Код для загрузки фрагмента из внешнего apk.

  final String libPath = Environment.getExternalStorageDirectory() + "/lib.apk";
        final File tmpDir = getDir("dex", 0);

        final File tmpDir1 = new File(libPath);
        Log.d(TAG, ""+tmpDir1.exists());

        final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
        final Class<Fragment> classToLoad = (Class<Fragment>) classloader.loadClass("com.example.sujeetkumarsinha.librarycode2.MyFragment");

Код ошибки

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sujeetkumarsinha.dynamic_code_run/com.example.sujeetkumarsinha.myapplication.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.sujeetkumarsinha.librarycode2.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4560)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.sujeetkumarsinha.librarycode2.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:449)
at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:110)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:2940)
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:149)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:287)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at com.example.sujeetkumarsinha.myapplication.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4560) 
at android.app.ActivityThread.-wrap19(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.sujeetkumarsinha.librarycode2.MyFragment" on path: DexPathList[[zip file "/data/app/com.example.sujeetkumarsinha.dynamic_code_run-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.sujeetkumarsinha.dynamic_code_run-1/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.support.v4.app.Fragment.instantiate(Fragment.java:439)
at android.support.v4.app.FragmentContainer.instantiate(FragmentContainer.java:33) 
at android.support.v4.app.FragmentState.instantiate(Fragment.java:110) 
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:2940) 
at android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:149) 
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:287) 
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85) 
at com.example.sujeetkumarsinha.myapplication.MainActivity.onCreate(MainActivity.java:26) 
at android.app.Activity.performCreate(Activity.java:6687) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4560) 
at android.app.ActivityThread.-wrap19(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

0 ответов

Другие вопросы по тегам