我的程序在我的启动画面后不断崩溃。为什么?

时间:2015-03-02 01:40:05

标签: android

我正在开发一个Android程序。它会显示启动画面,但在进入主活动之前会崩溃。这些是错误:

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)

这是例外的附近。最后一行是异常发生的地方。我没有得到它,我宣布适配器。:

  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();
        }

1 个答案:

答案 0 :(得分:0)

您需要移动此代码

 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);

因为您在super.onCreate(savedInstanceState);方法调用之前初始化了您的观看次数。