I'm trying to create a ListView with two separate items of TextViews.
My ListView is a part of content_main.xml (R.id.list), and TextViews are parts of list_item.xml (R.id.textUp, R.id.text2).
The error that I get is a ListView.setAdapter(android.widget.ListAdapter)' on a null object reference. This is how were assigned hashmap and listView in my java code:
private ListView list;
private HashMap<String, String> associatedItem;
protected void onCreate(Bundle savedInstanceState) {//LayoutInflater inflater
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_goal_text);
list = findViewById(R.id.list);
associatedItem = new HashMap<String, String>();
...
And the handler:
public void doneButtonHandler(View view) {
int day = picker.getDayOfMonth();
int month= picker.getMonth();
int year = picker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year,month,day);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", US);
String formatedDate = sdf.format(calendar.getTime());
associatedItem.put(formatedDate, strMssg);
ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
Iterator it = associatedItem.entrySet().iterator();
while (it.hasNext())
{
HashMap<String, String> resultsMap = new HashMap<>(2);
Map.Entry pair = (Map.Entry)it.next();
resultsMap.put("First Line",formatedDate);
resultsMap.put("Second Line",strMssg);//pair.getValue().toString()
listItems.add(resultsMap);
}
SimpleAdapter adapter = new SimpleAdapter(this, listItems ,R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.textUp, R.id.text2});
list.setAdapter(adapter);
}
Error Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.gryzhuk.goaltracker.AddGoal.doneButtonHandler(AddGoal.java:134)
activity_add_goal_text.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddGoal"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="28dp"
android:layout_marginBottom="23dp"
android:text="@string/enter_goal"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:onClick="nextButtonHandler"
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="53dp"
android:text="@string/next_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enteredGoalTxt"
app:layout_constraintVertical_bias="1.0" />
<EditText
android:id="@+id/enteredGoalTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="126dp"
android:ems="10"
android:hint="Enter here"
android:inputType="textShortMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
list_item.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:id="@+id/textUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorLineUp"
android:textSize="21sp"
android:textStyle="bold"
/>
<TextView android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/colorLineBottom"
android:textStyle="bold"
/>
</LinearLayout>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.teamproblemsolver.fabsubmenu.MainActivity">
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
fab:menu_animationDelayPerItem="50"
fab:menu_backgroundColor="@android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="@color/colorPrimaryDark"
fab:menu_colorPressed="#c784d3ed"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_fab_hide_animation="@anim/hide_to_bottom"
fab:menu_fab_show_animation="@anim/show_from_bottom"
fab:menu_fab_size="normal"
fab:menu_icon="@drawable/fab_add"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66000000"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="3dp"
fab:menu_showShadow="true">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/add_to_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_to_list"
fab:fab_colorNormal="@color/colorPrimary"
fab:fab_colorPressed="#c784d3ed"
fab:fab_label="Add Goal"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Double check the following method call:
You said, that the list is in the content_main.xml but in your code there is no reference to that layout file. Did you want to use this in the call to setContentView? Or is that an included layout or a fragment?
Also, even though it's a bit more complicated to use than listview, recyclerview is the new way to go to display a list of items in Android.