0

I have searched SO and it seems like the solutions to other problems like mine aren't doing it for me.

I am getting a NullPointerException on my setAdapter(). Here is the code:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_file);

    String currentDir = getApplicationContext().getFilesDir().getAbsolutePath();

    File file[] = GetFiles(currentDir);
    ArrayList<String> files = getFileNames(file);

    final ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, files);

    listView = (ListView)findViewById(android.R.id.list);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent myActivity = new Intent(SelectFile.this, MyActivity.class);
            myActivity.putExtra("filename", String.valueOf(listView.getItemAtPosition(position)));
            startActivity(myActivity);
            finish();
        }
    });
}

And the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="edu.niu.cs.jacob.journal.SelectFile"
android:id="@+id/relativeLay">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Select A File"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:layout_centerHorizontal="true"
    android:layout_below="@android:id/list"/>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at edu.niu.cs.jacob.journal.SelectFile.onCreate(SelectFile.java:44)
    at android.app.Activity.performCreate(Activity.java:6221)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

I have narrowed it down to the fact that the ListView is still null when it is initialized with findViewById. I have checked to make sure it is referring to the correct element in the correct XML file. What else could be causing this issue?

jww
  • 97,681
  • 90
  • 411
  • 885
Jacob Malachowski
  • 911
  • 1
  • 9
  • 18

2 Answers2

1

This is used for ListActivity: android.R.id.list.

Change:

listView = (ListView)findViewById(android.R.id.list);

To:

listView = (ListView)findViewById(R.id.list);

Please see this similar question: Your content must have a ListView whose id attribute is 'android.R.id.list'

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
1

This is because you are not using your own R. Change the findViewById to

listView = (ListView)findViewById(R.id.list);
Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30