我正在尝试使用带有 4 个小部件的 tabhost,我已经在 android 4.2 上对此进行了测试,它的工作原理就像一个魅力,但是在 android 2.2 上,当我更改标签时它给了我一个 nullpointerexception
我知道 tabhosts 和 android 2.1/2.2 存在一个已知问题,但是我似乎无法让它与人们在其他线程上建议的其他修复程序一起使用。
注意:我正在使用Android 注释
这是 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;
}
}