Имеется такой код: public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DialogFr dialogFragment = new DialogFr();
dialogFragment.show(getSupportFragmentManager(), "dialog");
}
}
class DialogFr extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setTitle("Фрукт: ")
.setMessage("Находится в: ")
.setNeutralButton("Положить", null)
.setNeutralButton("достать", null)
.setNegativeButton("Закрыть", null);
return dialog.create();
}
}
при его запуске происходит сбой, что я не так делаю?
ЛогиI Debugger is no longer active
2023-10-04 20:13:53.146 14583-14588 zygote com.example.myapplication I Do partial code cache collection, code=29KB, data=21KB
2023-10-04 20:13:53.146 14583-14588 zygote com.example.myapplication I After code cache collection, code=29KB, data=21KB
2023-10-04 20:13:53.146 14583-14588 zygote com.example.myapplication I Increasing code cache capacity to 128KB
2023-10-04 20:13:53.147 14583-14583 AppCompatDelegate com.example.myapplication D Checking for metadata for AppLocalesMetadataHolderService : Service not found
2023-10-04 20:13:53.254 14583-14583 AndroidRuntime com.example.myapplication D Shutting down VM
2023-10-04 20:13:53.255 14583-14583 AndroidRuntime com.example.myapplication E FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 14583
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.IllegalStateException: Fragment com.example.myapplication.DialogFr must be a public static class to be properly recreated from instance state.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: Fragment com.example.myapplication.DialogFr must be a public static class to be properly recreated from instance state.
at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:249)
at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:183)
at androidx.fragment.app.FragmentTransaction.add(FragmentTransaction.java:171)
at androidx.fragment.app.DialogFragment.show(DialogFragment.java:259)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)