简体   繁体   中英

Orientation change crashes my app

I have a class for constructing AlertDialog that contains spinner widget. When spinner shows its popup, if I change phone's orientation, my app crashes with exception saying something about leaked window. This matter has been discussed many times before, but only in the context of multithreading. But I have one thread. So what am I doing wrong?

public class ExpenseDialog extends DialogFragment
{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    LayoutInflater factory = LayoutInflater.from(this.getActivity());
    View content = factory.inflate(R.layout.expensedialog, null);

    Spinner spinner = (Spinner) content.findViewById(R.id.catspinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this.getActivity(), R.array.cats, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    return  new AlertDialog.Builder(this.getActivity())
        .setView(content)
        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
//some code
            }
        })
        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
//some code
            }
        })
        .create();
}

}

I show the dialog like this: (new ExpenseDialog()).show(getSupportFragmentManager(), "expensedialog");

There is a bug report related to this: Bug Report

Sorry, I can't help you to solve it --- I'm suffering from the same problem.

EDIT: Comment #7 worked for me. It didn't initially because I was using a DialogFragment and should have used onDestroyView() instead.

Check out this link on Handling Runtime Changes , it will help you understand what needs to be done for orientation changes. When you change the orientation, you app is detroyed by onDestroy() and restarted with onCreate() , which can lead to issues.

http://developer.android.com/guide/topics/resources/runtime-changes.html

尝试在显示此对话框的位置的活动清单文件中添加以下属性...

android:configChanges="keyboardHidden|orientation"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM