0

I am currently trying to create a master-details page using a list view in android studio.

I have created the list view, created a string array to be put into it, called the adapter and all the code looks good.

However when I open activity within the application that contains the list view the application crashes and says "Unfortunately the application has stopped working".

I'll attach the error code and my actual code below. Any help would be greatly appreciated.

My code

public class PlaceList extends AppCompatActivity {

ListView listView;

ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(PlaceList.this,
        android.R.layout.simple_list_item_1,
        getResources().getStringArray(R.array.countries));

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


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

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView,View view,int i,long l){
        Intent intent = new Intent(PlaceList.this,DetailsActivity.class);
        intent.putExtra("CountryName",listView.getItemAtPosition(i).toString());
        startActivity(intent);

        }
    });


    listView.setAdapter(mAdapter);



}

**Error Report on crash **

03-01 15:21:14.443 13699-13699/com.example.bradwaterhouse.discovernottingham 
E/AndroidRuntime: FATAL EXCEPTION: main

 Process: com.example.bradwaterhouse.discovernottingham, PID: 13699

 java.lang.RuntimeException: Unable to instantiate activity 

java.lang.NullPointerException:

 Attempt to invoke virtual method 'android.content.res.Resources 
 android.content.Context.getResources()' on a null object reference

 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3132)

 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:7331)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

at android.content.ContextWrapper.getResources(ContextWrapper.java:92)

at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)

at 

 android.support.v7.app.AppCompatActivity.getResources
(AppCompatActivity.java:542)

at com.example.bradwaterhouse.discovernottingham.PlaceList.<init>
(PlaceList.java:17)

at java.lang.Class.newInstance(Native Method)

at android.app.Instrumentation.newActivity(Instrumentation.java:1096)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 

at android.app.ActivityThread.access$1100(ActivityThread.java:229) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 

at android.os.Handler.dispatchMessage(Handler.java:102) 

at android.os.Looper.loop(Looper.java:148) 

at android.app.ActivityThread.main(ActivityThread.java:7331) 

 at java.lang.reflect.Method.invoke(Native Method) 

 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1230) 

 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
K.Os
  • 5,123
  • 8
  • 40
  • 95
Brad Waterhouse
  • 263
  • 3
  • 13
  • 2
    `mAdapter = new ArrayAdapter(PlaceList.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.countries));` write this line inside oncreate – Manohar Mar 01 '18 at 15:30
  • Sorted, cheers mate. If you submit an answer ill tick it for you. – Brad Waterhouse Mar 01 '18 at 15:33

2 Answers2

0

You need initial Adapter in onCreate() method.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
0

As mentioned in comments above

mAdapter = new ArrayAdapter<String>(PlaceList.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.countries));

Just write this line inside oncreate

Reason

You can only use your activity as a Context with Resources in onCreate() or later in the activity lifecycle

Credit laalto

Manohar
  • 22,116
  • 9
  • 108
  • 144