我正在创建一个 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);
}
我已经膨胀到主要活动中的 TabAcitity 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 exiting due to unaught exception 07-16 15:41:18.247: ERROR/AndroidRuntime(352): java.lang.RuntimeException: Unable to开始活动 ComponentInfo{com.bluelightuniverse.android.brewmobile/com.bluelightuniverse.android.brewmobile.RecipeGrainActivity}: java.lang.NullPointerException 07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.app.ActivityThread .performLaunchActivity(ActivityThread.java:2401) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.app.ActivityThread.access$2100(ActivityThread.java:116) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.app.ActivityThread$H.handleMessage(ActivityThread .java:1794) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-16 15:41:18.247: 错误/AndroidRuntime(352) : 在 android.os.Looper.loop(Looper.java:123) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.app.ActivityThread.main(ActivityThread.java:4203) 07-16 15:41:18.247: ERROR/AndroidRuntime(352): at java.lang.reflect.Method.invokeNative(Native Method) 07-16 15:41:18.247: ERROR/AndroidRuntime(352): at java.lang.reflect。 Method.invoke(Method.java:521) 07-16 15:41:18.247: 错误/AndroidRuntime(352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.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.NativeStart.main(Native Method) 07-16 15:41:18.247: ERROR/AndroidRuntime(352):
引起:java.lang.NullPointerException 07-16 15:41:18.247: ERROR/AndroidRuntime(352): at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 07-16 15:41:18.247: ERROR/ AndroidRuntime(352):在 com.bluelightuniverse.android.brewmobile.RecipeTabs.(RecipeTabs.java:17) 07-16 15:41:18.247:错误/AndroidRuntime(352):在 com.bluelightuniverse.android.brewmobile.RecipeGrainActivity。 setupView(RecipeGrainActivity.java:20)07-16 15:41:18.247:错误/AndroidRuntime(352):在 com.bluelightuniverse.android.brewmobile.RecipeGrainActivity.onCreate(RecipeGrainActivity.java:15)07-16 15:41: 18.247: 错误/AndroidRuntime(352): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 android.app.ActivityThread。performLaunchActivity(ActivityThread.java:2364)
它告诉我 TabActivity 类中的以下行有问题:
Resources res = getResources();
我敢肯定,这是它会检测到的许多错误中的第一个,因为我认为我没有将类正确地“膨胀”到活动中以设置选项卡。