rocket_doge_ rocket_doge_ - 1 month ago 7
Android Question

Android: NullPointerException when adding an Adapter to a ListView

I am creating a ViewPager in

activity_table.xml
.

This ViewPager has two pages which both share the same layout:
list.xml
.

This layout contains a
ListView
which I am trying to create an Adapter for.

The app compiles correctly but crashes when it tries to add the said Adapter to the
ListView
with a


NullPointerException when attempting to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference.


I have heard that I have to "load" a layout before I can use the Views it contains, but I how do I do that?

I have tried
setContentView(R.layout.list)
, but it throws the same error...

Table.java

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(sectionsPagerAdapter);

String[] tmp = new String[mainTable.length-2];
ListView listView = (ListView)findViewById(R.id.listView);
ListAdapter listAdapter = new ListAdapter(this,R.layout.list_item,tmp);
listView.setAdapter(listAdapter);

public static class TableFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section number";

public TableFragment() {
}
public static TableFragment newInstance(int sectionNumber) {
TableFragment fragment = new TableFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER,sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list,container,false);
TextView title = (TextView) rootView.findViewById(R.id.title);

if(getArguments().getInt(ARG_SECTION_NUMBER) == 1)
title.setText("Heute");
else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2)
title.setText("Morgen");

return rootView;
}
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return TableFragment.newInstance(position + 1);
}

@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "today";
case 1:
return "tomorrow";
}
return null;
}
}


public class ListAdapter extends ArrayAdapter<String> {
public ListAdapter(Context context, int textViewResourceId) {
super(context,textViewResourceId);
}
public ListAdapter(Context context, int resource, String[] items) {
super(context,resource,items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

if(v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item, null);
}

String p = getItem(position);

if(p!=null) {
TextView grade = (TextView) v.findViewById(R.id.grade);
TextView hour = (TextView) v.findViewById(R.id.hour);
TextView course = (TextView) v.findViewById(R.id.course);
TextView teacher = (TextView) v.findViewById(R.id.teacher);
TextView room = (TextView) v.findViewById(R.id.room);
TextView description = (TextView) v.findViewById(R.id.description);

try {
grade.setText(tableData[position][1]);
hour.setText(tableData[position][2]);
course.setText(tableData[position][3]);
teacher.setText(tableData[position][4]);
room.setText(tableData[position][5]);
description.setText(tableData[position][6]);
} catch (ArrayIndexOutOfBoundsException e) {
return v;
}
}

return v;
}
}


activity_table.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="de.fuchstim.vertretungsplan.Table">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">

</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:layout_gravity="bottom|right"
app:srcCompat="@drawable/reload_arrow_white" />


</android.support.design.widget.CoordinatorLayout>


list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Heute (25.10.16)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
android:textAlignment="center" />

<GridLayout android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/grade"
android:layout_height="wrap_content"
android:text="Klasse"
android:layout_row="0"
android:layout_column="0"
android:layout_width="0dp"
android:textSize="14sp"
android:layout_columnWeight="40"
android:paddingStart="10dp" />

<TextView
android:text="Stunde"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/hour"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="15"/>

<TextView
android:text="Lehrer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/teacher"
android:layout_row="0"
android:layout_column="3"
android:layout_columnWeight="15"/>

<TextView
android:text="Kurs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/course"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="15"/>

<TextView
android:text="Raum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/room"
android:layout_row="0"
android:layout_column="4"
android:layout_columnWeight="15"/>

</GridLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:id="@+id/listView" />

</LinearLayout>

Answer

I have tried setContentView(R.layout.list), but it throws the same error...

First, you need to use setContentView(R.layout.activity_table) instead.

Second, you cannot find the ListView from the Activity, since it is contained within the Fragment layout.

See here? Your ListView is in the same layout as this title element, so you need to find the ListView here...

View rootView = inflater.inflate(R.layout.list,container,false);
TextView title = (TextView) rootView.findViewById(R.id.title);

Move that ListView code from the Activity to the Fragment. And use getActivity() for the Adapter within the Fragment instead of this to get the Context.

That is where the @+id/listView value exists. That layout is searched with rootView.findViewById.

Comments