无法实例化片段-找不到Fragment构造函数

时间:2019-12-03 13:20:17

标签: android android-studio android-fragments

我正在尝试调用一个片段,但是即使我的片段中有空构造函数,有时也会收到以下错误

public MainFragment()
{

}


Caused by androidx.fragment.app.Fragment$e: Unable to instantiate fragment MainFragment: could not find Fragment constructor
   at androidx.fragment.app.Fragment.a(Fragment.java:94)
   at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java)
   at androidx.fragment.app.FragmentManagerImpl$6.instantiate(FragmentManagerImpl.java:9)
   at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:13)
   at androidx.fragment.app.FragmentManagerImpl.a(FragmentManagerImpl.java:239)
   at androidx.fragment.app.FragmentController.a(FragmentController.java:8)
   at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:17)
   at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:10)
   at me.dozee.dozee.MainActivity.onCreate(MainActivity.java)
   at android.app.Activity.performCreate(Activity.java:7149)
   at android.app.Activity.performCreate(Activity.java:7140)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182)
   at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6898)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

如果您不需要在MainFragment构造函数中执行任何操作,只需将其删除。

如果您确实需要重写构造函数,则应包括对super()

的调用
public MainFragment() {
    super()
}