Help with NullPointerException on API level 5
I developed my application using API level 8 but the minimum SDK is 3. I did most of the testing on an emulator running 8 and my DroidX as well as some friends phones. No problems thus far. It should also be known that I did not use any API's above Level 3, and the Tab Layout was almost and exact copy of the example provided by Google here. So.... when running on an emulator with level 5 API I get the following error and I have no clue where it's coming from except for the references to TabWidget.
Uncaught handle开发者_如何转开发r: thread main exiting due to uncaught exception
java.lang.NullPointerException
at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.View.draw(View.java:6539)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.View.draw(View.java:6539)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
at android.view.ViewRoot.draw(ViewRoot.java:1348)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1113)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1632)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4310)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, parent.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("browse").setIndicator("Browse",
res.getDrawable(R.drawable.ic_tab_browse))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, bookmarks.class);
spec = tabHost.newTabSpec("bookmarks").setIndicator("Bookmarks",
res.getDrawable(R.drawable.ic_tab_bookmarks))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, search.class);
spec = tabHost.newTabSpec("search").setIndicator("Search",
res.getDrawable(R.drawable.ic_tab_search))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
My tabhost was showing nullPointer exception for 2.1 updated at the time when i kept it in linearLayout,but when i tried Relative layout it workes fine . so might be this change help u if stucking in same situation .
With no code to work with, it is difficult to give you much advice.
If you are not using
TabActivity
, make sure you callsetup()
on yourTabHost
before doing anything else with it.Since the exception is coming from
TabWidget
, try temporarily simplifying your tab indicators and see if that helps localize or clear up the problem.
精彩评论