android – NullPointerException当创建一个新的对话框

前端之家收集整理的这篇文章主要介绍了android – NullPointerException当创建一个新的对话框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个DialogFragment创建一个listview对话框,并在一个列表项目单击我想显示一个警告对话框,但是当我创建对话框,它给我一个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)

这是我的Dialog的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",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());

这个错误是什么意思?

解决方法

这可能是因为您正在尝试从onClick Listener中调用getActivity().通常匿名内部类只能访问最终的内容.尝试在创建您的监听器之前做一些事情
final Context context = getActivity();

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

和作为一个说明.我会在这里使用DialogFragment.它更好地管理对话框的生命周期.当对话框打开时,当您尝试旋转屏幕时,您将遇到问题.但是对于DialogFragment来说,它会为你照顾.

原文链接:https://www.f2er.com/android/311812.html

猜你在找的Android相关文章