Я пытаюсь создать ListView с двумя отдельными элементами TextViews.
Мой ListView является частью content_main.xml (R.id.list), а TextViews являются частями list_item.xml (R.id.textUp, R.id.text2).
Я получаю ошибку ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
. Вот как были назначены hashmap и listView в моем коде Java:
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>();
...
И обработчик:
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);
}
Ошибка 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>