9

我有一个 DialogFragment,它创建一个列表视图对话框,在列表项上单击我想显示一个警报对话框,但是当我创建对话框时,它给了我一个NullPointerException我以前从未见过的错误

08-05 11:40:42.315: E/AndroidRuntime(4693): java.lang.NullPointerException
08-05 11:40:42.315: E/AndroidRuntime(4693):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at com.tyczj.bowling.dialogs.SeasonType$1$1.onClick(SeasonType.java:60)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at android.os.Looper.loop(Looper.java:137)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 11:40:42.315: E/AndroidRuntime(4693):     at dalvik.system.NativeStart.main(Native Method)

这是我的对话框onCreate

@Override
public Dialog onCreateDialog(Bundle state){
    final Dialog d = new Dialog(getActivity());
    d.setContentView(R.layout.dialog_layout);
    d.setTitle("Select a season");
    ListView lv = (ListView)d.findViewById(R.id.dialog_list);
    String[] list = getResources().getStringArray(R.array.season_dialog_type);
    lv.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.stats_list_layout,list));
    lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            d.dismiss();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if(arg2 == 0){
                DialogFragment df = new SeasonsDialog(true);
                df.show(ft,null);
            }else if(arg2 == 1){
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                final View v = getActivity().getLayoutInflater().inflate(R.layout.add_season_layout, null, false);
                builder.setTitle("Add a season").setView(v).setPositiveButton("Add", new OnClickListener(){

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        arg0.dismiss();
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity()); //Error here
                        builder2.setTitle("Warning!").setMessage("Adding a new season will cause all new games to be under this season.\n\n Do you wish to continue?")
                        .setPositiveButton("Yes", new OnClickListener(){

                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                dialog.dismiss();
                                EditText et = (EditText)v.findViewById(R.id.editText1);
                                ContentValues values = new ContentValues();
                                values.put(Games.SEASON, et.getText().toString());
                                Uri uri = getActivity().getContentResolver().insert(Games.SEASONS_URI, values);
                                et.setText("");
                                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
                                SharedPreferences.Editor edit = pref.edit();
                                edit.putLong(Preferences.SELECTED_SEASON, ContentUris.parseId(uri)).commit();
                            }

                        }).setNegativeButton("No", new OnClickListener(){

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

                        }).create().show();
                    }

                }).setNegativeButton("Cancel", new OnClickListener(){

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

                }).create().show();
            }

        }

    });

    return d;
}

它的错误出现在我的这一行onClick

AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity());

这个错误是什么意思?

4

2 回答 2

14

这可能是因为您试图从 onClick 侦听器中调用 getActivity()。通常匿名内部类只喜欢访问最终的东西。在创建你的听众之前尝试做类似的事情

final Context context = getActivity();

然后在崩溃的行上使用上下文而不是 getActivity() 。

并作为说明。我会在这里使用DialogFragment。它可以更好地管理 Dialog 的生命周期。当您在对话框打开时尝试旋转屏幕时会遇到问题。但是,使用 DialogFragment 它将为您解决这个问题。

于 2012-08-05T16:06:07.683 回答
2

试试这个

AlertDialog alrt = (AlertDialog )arg0;
AlertDialog.Builder builder = new AlertDialog.Builder(alrt.getContext());

在显示新对话框后也关闭旧对话框。

于 2012-08-05T16:09:36.683 回答