I was recently working on a todo list app.
The code for the app's OnCreate method is shown below:
public class MainActivity extends AppCompatActivity {
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ListView todo = (ListView) findViewById(R.id.list);
items = new ArrayList<>();
itemsAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, items);
todo.setAdapter(itemsAdapter);
ActionBar ab = getSupportActionBar();
assert ab != null;
ab.hide();
}
But when I run the app, it keeps crashing.
I checked the logcat to see what the error was and I got the following piece.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dobleu.hotlists/com.dobleu.hotlists.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
I have no idea why I'm getting a NPE.
I am using fragments and I am utilizing a BottomNavigationView to transition between them.
Here is the xml for the fragment with the listview:
<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">
<LinearLayout
android:layout_width="0dp"
android:layout_height="232dp"
android:background="@drawable/header1"
android:scaleType="fitStart"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/imageView"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:background="#e95555"
android:id="@+id/linearLayout"
android:orientation="vertical">
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="13dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:background="#e95555"
app:fabSize="normal"
android:onClick="AddItem"/>
</android.support.constraint.ConstraintLayout>
And here's the xml for the main activity:
<RelativeLayout 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="com.dobleu.hotlists.MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#2b2b2b"
app:itemIconTint="#fff"
app:itemTextColor="#fff"
app:menu="@menu/menu"/>
</RelativeLayout>
I have tried a lot to get it working but I have not been able to find the solution.
I've read that post on what a NPE is and why, but I still don't understand.
And now I'm sick and tired that people keep marking my questions as duplicates when I am very well aware that there is an answer that can apply to this question but I have no idea how to apply it.
Please help me.