我正在创建一个TabActivity菜单在整个应用程序中使用。是否有一种方法可以在一个类中构建菜单,然后将其扩展到我想要使用它的每个活动中?
我想将选项卡菜单膨胀为:
private void setupView() {
LayoutInflater inflater = (LayoutInflater)RecipeGrainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RecipeTabs tabs = new RecipeTabs(this, 1);
View tabView = inflater.inflate(R.layout.recipe_tabs, null);
}
我已经填充到主活动中的tabactivity xml布局:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout> </TabHost>
我已经创建了TabActivity类来设置我所有的选项卡:
public class RecipeTabs extends TabActivity {
private Context myContext;
private int currentTab;
public RecipeTabs(Context context, int currentTab) {
this.myContext = context;
this.currentTab = currentTab;
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
//Grain Tab
intent = new Intent().setClass(context, RecipeGrainActivity.class);
spec = tabHost.newTabSpec("grain").setIndicator("Grain", res.getDrawable(R.drawable.grain_icon_states))
.setContent(intent);
tabHost.addTab(spec);
//Hops Tab
intent = new Intent().setClass(context, RecipeGrainActivity.class);
spec = tabHost.newTabSpec("hops").setIndicator("Hops", res.getDrawable(R.drawable.hops_icon_states))
.setContent(intent);
tabHost.addTab(spec);
//Mash Tab
intent = new Intent().setClass(context, RecipeGrainActivity.class);
spec = tabHost.newTabSpec("mash").setIndicator("Mash", res.getDrawable(R.drawable.mash_icon_states))
.setContent(intent);
tabHost.addTab(spec);
//Notes Tab
intent = new Intent().setClass(context, RecipeGrainActivity.class);
spec = tabHost.newTabSpec("notes").setIndicator("Notes", res.getDrawable(R.drawable.notes_icon_states))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(currentTab);
}
}
当我尝试运行活动时,我在LogCat中得到以下错误:
07-16 15:41:18.237: ERROR/AndroidRuntime(352): Uncaught handler: thread main exit to Uncaught exception07-16 15:41:18.247: ERROR/AndroidRuntime(352): java.lang.RuntimeException: able to start activity ComponentInfo{com. bluelightuniversse .android.brewmobile/com. bluelightuniversse .android.brewmobile. com. bluelightuniversse .android.brewmobile. com. bluelightuniversse .android.brewmobile. htmRecipeGrainActivity}: java.lang.NullPointerException07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread.access$2100(ActivityThread.java:116)07:16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.os.Handler.dispatchMessage(Handler.java:99)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.os. loop .loop(loop .java:123)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread.main(ActivityThread.java:4203)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at java.lang.reflect.Method。invokeNative(本地方法)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at java.lang.reflect.Method.invoke(Method.java:521)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at com.android.internal.os.ZygoteInit$ methodandargscallle .run(ZygoteInit.java:791)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at dalvik.system. native . art. exe主要(本地方法)07-16 15:41:18.247: ERROR/AndroidRuntime(352):
肇因:java.lang.NullPointerException 07-16 15:41:18.247:错误/AndroidRuntime (352):android.content.ContextWrapper.getResources (ContextWrapper.java: 80)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at . execom.bluelightuniverse.android.brewmobile.RecipeTabs。(RecipeTabs.java: 17)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at . execom.bluelightuniverse.android.brewmobile.RecipeGrainActivity.setupView (RecipeGrainActivity.java: 20)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at . execom.bluelightuniverse.android.brewmobile.RecipeGrainActivity.onCreate (RecipeGrainActivity.java: 15)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at . exeandroid.app.Instrumentation.callActivityOnCreate (Instrumentation.java: 1123)07-16 15:41:18.247: ERROR/AndroidRuntime(352): at . exeandroid.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2364)
它告诉我,我有一个问题与TabActivity类中的以下行:
Resources res = getResources();
我敢肯定,这是它会检测到的许多错误中的第一个,因为我认为我没有正确地将类"膨胀"到活动中,以便设置选项卡。
我像往常一样想得太多了。我最终将TabActivity设置为主活动,而不是试图将其设置为子类。TabHost自动加载你的"链接"活动,它很容易实现。其他需要查找的资源是Fragments,因为它遵循相同的原则,从外观上看,它在应用程序中非常有用。