0

我有一个FragmentActivity使用SherlockActionBar和支持库 v4。

主布局中有一个ListFragment。点击一个列表项会打开一个新ListFragment的部分滑入视图。如果您长按这些列表项,则会显示一个包含两个选项的上下文菜单。第一个选项显示一个dialog带有一个EditText框和一个Button' 它在尝试打开时崩溃。

如果我删除该EditText行和这些Button行(除非您用 声明它们),会弹出findViewById一个空的。dialog但出于某种原因,它不喜欢对话框的内容(按钮/框)。任何人都可以查看 LogCat 并了解原因吗?线就是null线setText。如果我将其注释掉,按钮将onClickListner变为null.

这是该代码和logcat的代码。

public boolean onContextItemSelected(android.view.MenuItem item) {

        if (item.getGroupId() == 2) {

            switch (item.getItemId()) {

            case 1:


case 1:

                if (Rateit.isUserLoggedIn == true) {

                    final Dialog dialog = new Dialog(getActivity());
                    dialog.setTitle("Edit Review");
                    dialog.show();

                    final EditText etEdit = (EditText) dialog
                            .findViewById(R.id.etEditReview);

                    etEdit.setText(reviewWords);

                    Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
                    bInsert.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {

                            clickedReview = etEdit.getText().toString();

                            if (clickedReview.equals("")) {

                                Toast.makeText(getActivity(),
                                        "Please add something first.",
                                        Toast.LENGTH_SHORT).show();

                            } else {
                                newReview = etEdit.getText().toString();
                                new EditCommentTask().execute();

                                InputMethodManager imm = (InputMethodManager) getActivity()
                                        .getSystemService(
                                                Context.INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(
                                        etEdit.getWindowToken(), 0);

                                dialog.dismiss();

                            }

                        }
                    });

                }

                return true;

日志猫:

03-06 10:29:08.527: E/AndroidRuntime(10438): FATAL EXCEPTION: main
03-06 10:29:08.527: E/AndroidRuntime(10438): java.lang.NullPointerException
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.---.---.RateReviewFragment.onContextItemSelected(RateReviewFragment.java:901)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:1933)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:360)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:211)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3633)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.os.Handler.handleCallback(Handler.java:725)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.os.Looper.loop(Looper.java:137)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at android.app.ActivityThread.main(ActivityThread.java:5226)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-06 10:29:08.527: E/AndroidRuntime(10438):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

看起来好像你没有膨胀对话框的 XML,我想你有那个对话框的 XML,所以你应该做这样的事情:

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.instructions, (ViewGroup) activity.findViewById(R.id.layout_root));
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
Button bInsert = (Button) layout.findViewById(R.id.bInsert);
alertDialog = builder.create();
alertDialog.show();

你会放在哪里

 mContext=this; 

在 onCreate() (如果你在你的活动中)

于 2013-03-06T18:57:10.923 回答
1

查看您的代码,您似乎忘记为新创建的Dialog(使用)设置布局(包含您的小部件setContentView(the_layout_file))。如果您不这样做,那么在对话框中搜索并尝试使用这些小部件将抛出一个NullPointerException.

于 2013-03-07T07:00:09.797 回答