开发者

GridView weird error

I have a weird error concerning my gridview. It was working fine until it crashed for no reason. Below are the error codes:

04-21 14:00:08.610: ERROR/AndroidRuntime(578): FATAL EXCEPTION: main  
04-21 14:00:08.610: ERROR/AndroidRuntime(578): java.lang.RuntimeException: Unable to start activity ComponentInfo{joel.TPLibrary/joel.TPLibrary.MainBody}: java.lang.NullPointerException  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.os.Handler.dispatchMessage(Handler.java:99)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.os.Looper.loop(Looper.java:123)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.main(ActivityThread.java:4627)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invokeNative(Native Method)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invoke(Method.java:521)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at dalvik.system.NativeStart.main(Native Method)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578): Caused by: java.lang.NullPointerException  
***04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at joel.TPLibrary.MainBody.onCreate(MainBody.java:31)***  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
04-21 14:00:08.610: ERROR/AndroidR开发者_Go百科untime(578):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  
04-21 14:00:08.610: ERROR/AndroidRuntime(578):     ... 11 more  

Next is the line of error:

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

    gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // TODO Auto-generated method stub
            switch (position) {
                case 0:
                    Intent myIntentSearch = new Intent();
                    myIntentSearch.setClassName("joel.TPLibrary","joel.TPLibrary.Search");
                    startActivity(myIntentSearch);
                    break;
                case 1:
                    Intent myIntentFP = new Intent();
                    myIntentFP.setClassName("joel.TPLibrary","joel.TPLibrary.FloorPlan");
                    startActivity(myIntentFP);
                    break;
                case 2:
                    Intent myIntentNews = new Intent();
                    myIntentNews.setClassName("joel.TPLibrary","joel.TPLibrary.News");
                    startActivity(myIntentNews);
                    break;
                case 3:
                    Intent myIntenteRes = new Intent();
                    myIntenteRes.setClassName("joel.TPLibrary","joel.TPLibrary.eResources");
                    startActivity(myIntenteRes);
                    break;
                case 4:
                    Intent myIntentOH = new Intent();
                    myIntentOH.setClassName("joel.TPLibrary","joel.TPLibrary.OpeningHours");
                    startActivity(myIntentOH);
                    break;
                case 5:
                    Intent myIntentCU = new Intent();
                    myIntentCU.setClassName("joel.TPLibrary","joel.TPLibrary.ContactUs");
                    startActivity(myIntentCU);
                    break;
                case 6:
                    Intent myIntentLibFb = new Intent();
                    myIntentLibFb.setClassName("joel.TPLibrary","joel.TPLibrary.LibraryFacebook");
                    startActivity(myIntentLibFb);
                    break;
                case 7:
                    Intent myIntentNA = new Intent();
                    myIntentNA.setClassName("joel.TPLibrary","joel.TPLibrary.NewArrivals");
                    startActivity(myIntentNA);
                    break;
                case 8:
                    Intent myIntentAnn = new Intent();
                    myIntentAnn.setClassName("joel.TPLibrary","joel.TPLibrary.Announements");
                    startActivity(myIntentAnn);
                    break;
            }
        }
    });
}

Can anyone help me?


Since your Activity is accessed based on the

... at joel.TPLibrary.MainBody.onCreate(MainBody.java:31)*** 

line in your stack trace, the only things i can think of that might cause this problem are:

  • You have forgotten to set the content view for your Activity inside the onCreate() method, eg: setContentView(R.layout.main);
  • Your main.xml (referred as R.layout.main above) does not contain a <GridView android:id="@+id/gridview" [...] element.

Providing the whole source of your onCreate method, and marking the 31. line would help though detecting the exact problem, not just guessing. Please share more information / respond to your comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜