Android之提示Unable to instantiate fragment***MyLikeFragment .could not find Fragment constructor

1 问题

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appsinnova.android.keepdrop/com.appsinnova.android.keepdrop.account.favorite.activity.MyLikeActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.appsinnova.android.keepdrop.fragment.MyLikeFragment: could not find Fragment constructor
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.appsinnova.android.keepdrop.fragment.MyLikeFragment: could not find Fragment constructor
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:538)
        at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
        at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:2851)
        at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:85)
        at androidx.fragment.app.FragmentManagerImpl.restoreSaveState(FragmentManagerImpl.java:2488)
        at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:195)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:288)
        at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
        at com.trello.rxlifecycle2.components.support.RxAppCompatActivity.onCreate(RxAppCompatActivity.java:59)
        at com.skyunion.android.base.RxBaseActivity.onCreate(RxBaseActivity.java:131)
        at com.appsinnova.android.keepdrop.base.BaseActivity.onCreate(BaseActivity.kt:26)
        at android.app.Activity.performCreate(Activity.java:8086)
        at android.app.Activity.performCreate(Activity.java:8074)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3755)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3961) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8178) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 

 

 

 

 

2 引发原因

我在Activity里面传递参数给Fragment的时候,对Fragment的构造方法去掉了,然后加了一个类型的参数放入构造函数

constructor(type: String) {
    this.type =  type
}

这样很容易引发奔溃,如上,我们传递参数应该用setArguments方法通过bundle来传递需要的数据

 

 

 

 

 

3 解决办法

activity里面

        var fragment = MyLikeFragment()
        val bundle = Bundle()
        bundle.putString(EXTRA_INDEX, info)
        fragment.arguments = bundle
        showFragment(fragment)

fragment的onCreateView里面

 var type = getArguments()?.getString(EXTRA_INDEX)

 

 

 

 

4、 总结

Activity和Fragment里面传递数据尽量用setArguments方法

千万不要用 Fragment的构造函数传递参数,各种坑。

你可能感兴趣的:(Andriod,积累,Android,could,not,find,Fragment,constructor)