0

I am working on an android program. It shows the splash screen but then crashes before it gets to the main activity. These are the errors:

03-01 22:48:10.473    2657-2657/com.example.user.checkbox E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.user.checkbox, PID: 2657
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.checkbox/com.example.user.favoritebooks.Main}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
            at android.app.ActivityThread.access$900(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5487)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.user.favoritebooks.Main.onCreate(Main.java:36)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
            at android.app.ActivityThread.access$900(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5487)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

This is the vicinity of the exception. The last line is where the exception happens. I don't get it, I declared adapter.:

  public static final String favorites = "MyUserChoice" ;
    ArrayList<String> items = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        getFav = (Button)findViewById(R.id.getchoice);
        savetoshared = getSharedPreferences(favorites, Context.MODE_PRIVATE);
        BookList = (ListView)findViewById(R.id.list);
        clearbutton = (Button)findViewById(R.id.clearall);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, getResources().getStringArray(R.array.Favorite_Books));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        BookList.setAdapter(adapter); //error
        BookList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        if(savetoshared.contains(favorites))
        {
            LoadSelections();
        }
munchschair
  • 1,593
  • 3
  • 19
  • 43

1 Answers1

0

You need to move this code

 getFav = (Button)findViewById(R.id.getchoice);
 savetoshared = getSharedPreferences(favorites, Context.MODE_PRIVATE);
 BookList = (ListView)findViewById(R.id.list);
 clearbutton = (Button)findViewById(R.id.clearall);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, getResources().getStringArray(R.array.Favorite_Books));

after

 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

because you'r initialize your views before super.onCreate(savedInstanceState); method call.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Basically you can't use `findViewsById` before `setContentView` and can't use `getString`, `getResources`, `setContentView` or pretty much anything before `super.onCreate`. – Eugen Pechanec Mar 02 '15 at 12:30