2015-02-24 123 views
0

我的問題是這樣的:我是新來的android編程,我創建了一個新的應用程序,當我按下按鈕啓動下面的這個活動,它崩潰。當我調試它時,它說onCreate()得到一個空包。我該如何解決?我不明白其他類的活動,所以請使用詳細信息,提前致謝!onCreate(savedInstanceState)savedInstanceState爲NULL

這裏是我的代碼:

@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_requests); 


     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 


    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(my_requests.this); 
      switch (position){ 
       case 0: builder.setMessage("You Chose Television, Wait"); 
        break; 
       case 1: builder.setMessage("You Chose Cookie, Wait"); 
        break; 
       case 2: builder.setMessage("You Chose Toilet, Wait"); 
        break; 
       case 3: builder.setMessage("You Chose Toys, Wait"); 
        break; 
       case 4: builder.setMessage("You Chose Lunch, Wait"); 
        break; 
       case 5:builder.setMessage("You Chose Water, Wait"); 
        break; 
      } 
      builder.setNeutralButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 
       } 
      }); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 

     } 
    }); 
    super.onSaveInstanceState(savedInstanceState); 
} 

編輯:: ADDED logcat的BELOW

02-24 14:53:00.642 17789-17789/com.tiktaktikapps.Gimmie_ D/AndroidRuntime﹕ Shutting down VM 
    --------- beginning of crash 
02-24 14:53:00.647 17789-17789/com.tiktaktikapps.Gimmie_ E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.tiktaktikapps.Gimmie_, PID: 17789 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tiktaktikapps.Gimmie_/com.tiktaktikapps.Gimmie_.my_requests}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367) 
      at android.app.ActivityThread.access$800(ActivityThread.java:148) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5274) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference 
      at com.tiktaktikapps.Gimmie_.my_requests.onCreate(my_requests.java:49) 
      at android.app.Activity.performCreate(Activity.java:5977) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367) 
            at android.app.ActivityThread.access$800(ActivityThread.java:148) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5274) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
+0

顯示logcat的 – 2015-02-24 11:14:51

+0

我認爲你的最後一行(的onSaveInstanceState)是什麼使你的應用程序崩潰 – programmer23 2015-02-24 11:17:16

+0

我做到了,它有final'從'的onCreate(最終捆綁savedInstanceState)沒有影響@ programmer23 – 2015-02-24 11:25:42

回答

1

所致:顯示java.lang.NullPointerException:嘗試上的空對象引用調用虛擬方法 '無效android.widget.GridView.setAdapter(android.widget.ListAdapter)'

您發佈logcat的摘錄無關用一個空包,而是表明R.id.gridview未在R.layout.my_requests

發現檢查你的XML文件,錯誤,錯別字,資本的差異,並確保你正在使用你的意思是那些至。然後清理你的項目(一些工具版本不擅長拾取XML更改)並重新部署。

0

刪除最後一個線 -

super.onSaveInstanceState(savedInstanceState); 

應該從onSavedInstanceState被稱爲()。

+0

刪除,但結果沒有變化... – 2015-02-24 11:26:45

+0

從onCreate刪除「final」(final Bundle savedInstanceState) – 2015-02-24 11:33:56

+0

仍然沒有變化 – 2015-02-24 11:56:56

相關問題