我想用4個小工具使用tabhost,我已經測試了這個在android 4.2上,它的工作原理就像是一個魅力,然而在android 2.2上它給了我一個nullpointerexception當我改變標籤TabHost在Android 2.2中的空指針異常
我知道有一個tabhosts和android 2.1/2.2的已知問題,但我似乎無法讓它與人們在其他線程上建議的其他修復程序一起工作。
注:我使用Android Annotations
這裏的logcat的:
03-23 10:29:08.869: E/AndroidRuntime(423): FATAL EXCEPTION: main
03-23 10:29:08.869: E/AndroidRuntime(423): java.lang.NullPointerException
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:367)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.view.View.performClick(View.java:2408)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.view.View$PerformClick.run(View.java:8816)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Handler.handleCallback(Handler.java:587)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Looper.loop(Looper.java:123)
03-23 10:29:08.869: E/AndroidRuntime(423): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 10:29:08.869: E/AndroidRuntime(423): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 10:29:08.869: E/AndroidRuntime(423): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 10:29:08.869: E/AndroidRuntime(423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 10:29:08.869: E/AndroidRuntime(423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 10:29:08.869: E/AndroidRuntime(423): at dalvik.system.NativeStart.main(Native Method)
這裏是我的代碼:
@NoTitle
@EActivity(R.layout.activity_base)
public class BaseActivity extends TabActivity {
Context mContext;
@ViewById
Button btnBaseDeconnecter;
@ViewById
TextView txtBaseInfos;
@AfterViews
void afterViews() {
mContext = this;
TabHost tabHost = getTabHost();
/* tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("0");
TabSpec secondTabSpec = tabHost.newTabSpec("1");
TabSpec thirdTabSpec = tabHost.newTabSpec("2");
TabSpec fourthTabSpec = tabHost.newTabSpec("3");
/* TabSpec setIndicator() is used to set name for the tab. */
/* TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator(
prepareTabView(mContext, getResources().getDrawable(R.drawable.enlevement))).setContent(
new Intent(this, EnlevementActivity_.class));
secondTabSpec.setIndicator(
prepareTabView(mContext, getResources().getDrawable(R.drawable.travaux))).setContent(
new Intent(this, TravauxListActivity_.class));
thirdTabSpec.setIndicator(
prepareTabView(mContext, getResources().getDrawable(R.drawable.compte))).setContent(
new Intent(this, CompteActivity_.class));
fourthTabSpec.setIndicator(
prepareTabView(mContext, getResources().getDrawable(R.drawable.contact))).setContent(
new Intent(this, ContactActivity_.class));
tabHost.setup();
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
tabHost.addTab(fourthTabSpec);
tabHost.getTabWidget().setStripEnabled(false);
tabHost.getTabWidget().setDividerDrawable(R.drawable.empty_divider);
}
public static View prepareTabView(Context context, Drawable background) {
View view = LayoutInflater.from(context).inflate(R.layout.fake_native_tab, null);
ImageView img = (ImageView)view.findViewById(R.id.fakeNativeTabImageView);
img.setImageDrawable(background);
return view;
}
}
是否發生這種異常?是否嘗試打開特定選項卡或嘗試打開任何選項卡或四個選項卡? – 2013-03-23 09:47:21
當應用程序按照您的預期啓動時,會在第一個選項卡上加載,但如果我點擊第二個選項卡,則會將其發送給第三個選項卡,並且當我單擊第三個或第四個選項卡時,它會崩潰 – 2013-03-23 09:51:31
Precsions:它發送我到第三個小部件,但到第二個類 – 2013-03-23 10:17:52