DialogFragment在方向更改时崩溃应用程序

时间:2013-12-30 12:01:54

标签: android android-fragments

我遇到AlertFragment的问题,当您第一次运行应用时,但是当您旋转设备时(方向已更改),应用程序崩溃了。

架构:MainActivity - > ScheduledRecListFragment - > AlertFragment

MainActivity(简化摘录):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   // < -- line 28
    setContentView(R.layout.activity_main);

    fragmentManager = getFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    ScheduledRecListFragment fragment = new ScheduledRecListFragment();

    if(getFragmentManager().findFragmentByTag("RecListFrag") == null) {

        fragmentTransaction.add(R.id.fragment_container, fragment, "RecListFrag");
        fragmentTransaction.commit();
    }

}

ScheduledRecListFragment(简化提取):

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.scheduled_rec_list_fragment, container, false);
    lv = (ListView) v.findViewById(R.id.listViewRecs);

   // ...

    if(db.selectAllRecords().getCount() == 0  )   {

        new AlertFragment(getString(R.string.alert), getString(R.string.no_recs)).show(getFragmentManager(), "MyDialog");
    }

   // ...

    return v;

}

AlertFragment:

class AlertFragment extends DialogFragment {

String title;
String message;


public AlertFragment(String title, String message)  {

    this.title = title;
    this.message = message;
}


public AlertFragment()  {


}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setMessage(message);
    //null should be your on click listener

    alertDialogBuilder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });


    return alertDialogBuilder.create();
}
}

logcat的:

12-30 12:35:27.421  11871-11871/com.app.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.example/com.app.example.MainActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.app.example.AlertFragment: make sure class name exists, is public, and has an empty constructor that is public
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3727)
            at android.app.ActivityThread.access$700(ActivityThread.java:146)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5171)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.app.example.AlertFragment: make sure class name exists, is public, and has an empty constructor that is public
            at android.app.Fragment.instantiate(Fragment.java:600)
            at android.app.FragmentState.instantiate(Fragment.java:97)
            at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1747)
            at android.app.Activity.onCreate(Activity.java:922)
            at com.app.example.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:5200)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3727)
            at android.app.ActivityThread.access$700(ActivityThread.java:146)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5171)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalAccessException: access to class not allowed
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Fragment.instantiate(Fragment.java:585)
            at android.app.FragmentState.instantiate(Fragment.java:97)
            at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1747)
            at android.app.Activity.onCreate(Activity.java:922)
            at com.app.example.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:5200)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3727)
            at android.app.ActivityThread.access$700(ActivityThread.java:146)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5171)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:6)

好的,我找到了......解决方法是将setRetainInstance(true)置于Dialog的{​​{1}}方法中。