Skip to content Skip to sidebar Skip to footer

Android: Attempt To Invoke Virtual Method 'void Android.widget.ListView.setAdapter(android.widget.ListAdapter)' On A Null Object Reference

I am trying to make a list view that contains pictures and text within another activity. I am not so focused in android programming, so basically like a newbie in android programmi

Solution 1:

You're missing one very important line - setContentView(). That line binds the xml with the Activity's view and it's the view that's being used for findViewById. Add this line to your onCreate:

@Override
public void onCreate(Bundle state){
    super.onCreate(state);
    setContentView(R.layout.name_of_the_xml);

Solution 2:

You have to add the layout of MainActivity (which contains the Listview) before use findViewById()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContent(R.layout_MAIN_LAYOUT_ACTIVITY);
    //Initialize View
    mListViewDID = (ListView) findViewById(R.id.list_did);
}

Post a Comment for "Android: Attempt To Invoke Virtual Method 'void Android.widget.ListView.setAdapter(android.widget.ListAdapter)' On A Null Object Reference"