TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Crash on `onContextItemSelected` above Fragment

I have a FragmentActivity using SherlockActionBar and support library v4.

There is a ListFragment in the main layout. Clicking on a list item opens up a new ListFragment that slides partially into view. If you long-click on THOSE list items, a context menu shows up with two options. The first option shows a dialog with an EditText box and a Button' it crashes when it tries to open.

If I delete the EditText line and the Button lines (except where you declare them with findViewById), an empty dialog does pop up. But it doesn't like the content (button/box) of the dialog for some reason. Can anyone look at LogCat and see why? The line that is null is the setText line. If I comment it out, the button's onClickListner becomes null.

Here is the code for that and the 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;

LogCat:

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)

Upvotes: 0

Views: 377

Answers (2)

user
user

Reputation: 87064

Looking at your code, it seems that you probably forgot to set the layout(containing your widgets) for the newly created Dialog(with setContentView(the_layout_file)). If you don't do this then searching in the dialog and attempting to use those widgets will throw a NullPointerException.

Upvotes: 1

Lisa Anne
Lisa Anne

Reputation: 4595

It looks as if you are failing to inflate the XML of the dialog, I suppose you have a XML for that dialog, so you should do something like this:

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();

where you would put

 mContext=this; 

in onCreate() (if you are in your Activity)

Upvotes: 1

Related Questions