0

My current structure involves a navDrawer with a list of items in it, pointing to the main fragment which has a RecyclerView containing CardView. When I am setting up the ListView to pass it to registerForContextMenu the app is crashing giving error that initializing object with null value.

The code that i'm using to declare the ListView item to pass is.

ListView LV = (ListView) findViewById(R.id.cv);

where "cv" is the id of the CardView element that holds the data to be displayed.

registerForContextMenu(LV);

At this point the particular error is coming:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnCreateContextMenuListener(android.view.View$OnCreateContextMenuListener)' on a null object reference

the value of LV is coming to be null. I'm new to android, am I missing out something in declaration or while I'm using cast to ListView? Long Click possible in CardView or not?

Gaurav Rai
  • 313
  • 1
  • 2
  • 10

2 Answers2

0

You are referring to wrong id with your listview.When you are initializing the list view you need to fins by using its id.It's the same with every other view.

In this case you are passing your card view id to your listview object which is incorrect.cv should be the id of your list view and not the card view.

It should be something like

ListView LV = (ListView) findViewById(R.id.ypur_list_view_id_in_xml);
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
  • Can I pass a CardView object to registerForContextMenu(), because I want to show the context menu on the long press of the CardView? – Gaurav Rai Jul 24 '15 at 13:51
  • i've no idea of context menu but i believe you can do a long press using your cardview on list.because the long press will be on one row of your list not on card view. – Anirudh Sharma Jul 24 '15 at 13:54
  • I'll tell you the entire structure, drawer layout contains FrameLayout(main display) and a ListView(sliding menu portion in left), in the FrameLayout I have a RecyclerView inside that I have the CardView that has the actual data on press of which I want the context view to appear, now tell me what should I pass cardView or List. If then which view should I cast into List? – Gaurav Rai Jul 24 '15 at 14:12
0

I found out one possible solution to this issue. In the RecyclerViewAdapter where you are creating the structure for the components that is going to appear on the display, make one class

public static class CompareViewHolder extends RecyclerView.ViewHolder implements RecyclerView.OnCreateContextMenuListener {
    CardView cv;
    TextView itemid;
    TextView baseline;
    TextView current;

    CompareViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        itemid = (TextView) itemView.findViewById(R.id.itemid);
        baseline = (TextView) itemView.findViewById(R.id.baseline);
        current = (TextView) itemView.findViewById(R.id.current);
        itemView.setOnCreateContextMenuListener(this);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    }
}

And in your main activity (where the RecyclerView is appearing)

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.my_context_menu, menu);
}

and then to the the selection thing working for the menu items override another method

@Override
public boolean onContextItemSelected(MenuItem item) {
    //Action to be done
}

my_context_menu.xml holds the options that will appear in the context menu.

Refer this How to create context menu for RecyclerView for further information if this not seems helpful to you.

I was forcibly converting the RecyclerView to ListView and then was registering that for the context menu (which is not required), the object was coming out to be null because the view is yet not created, the same thing is to be done without casting into ListView from your RecyclerViewAdapter that you are making.

Community
  • 1
  • 1
Gaurav Rai
  • 313
  • 1
  • 2
  • 10