我正在创建一个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):未捕获的处理程序:由于未捕获的异常而导致主线程退出 07-16 15:41:18.247:ERROR / AndroidRuntime(352):java.lang.RuntimeException:无法启动活动ComponentInfo {com.bluelightuniverse.android.brewmobile / com.bluelightuniverse.android.brewmobile.RecipeGrainActivity}:java.lang 。空指针异常 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.app.ActivityThread.access $ 2100(ActivityThread.java:116) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.os.Handler.dispatchMessage(Handler.java:99) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):在android.os.Looper.loop(Looper.java:123) 07-16 15:41:18.247:ERROR / 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:ERROR / 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: 错误/ AndroidRuntime(352):at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):at com.bluelightuniverse.android.brewmobile.RecipeTabs。(RecipeTabs.java:17) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):at com.bluelightuniverse.android.brewmobile.RecipeGrainActivity.setupView(RecipeGrainActivity.java:20) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):at com.bluelightuniverse.android.brewmobile.RecipeGrainActivity.onCreate(RecipeGrainActivity.java:15) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 07-16 15:41:18.247:ERROR / AndroidRuntime(352):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
它告诉我TabActivity类中的以下行存在问题:
Resources res = getResources();
我确定,这是它会检测到的许多错误中的第一个,因为我认为我没有正确地将类“充气”到活动中以设置标签。
答案 0 :(得分:0)
我正在过度思考(像往常一样)。我最终将TabActivity设置为主要活动,而不是尝试将其设置为子类。 TabHost自动加载“链接”活动,并且非常容易实现。要查找的其他资源是Fragments,因为它遵循相同的原则,从它的外观来看它在应用程序中非常有用。