我正在尝试调用片段,但有时会收到以下错误,即使我的片段中有空构造函数
如
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)
如果你不需要在MainFragment
构造函数中做任何事情,只需删除它。
如果确实需要重写构造函数,它应包括对super()
的调用
public MainFragment() {
super()
}