我正在創建一個TabActivity菜單,以便在我的應用程序中使用。有沒有辦法在一個類中構建菜單,然後將其充入我想要用於其中的每個活動?Android:將一個類充氣到另一個類
我要膨脹的選項卡菜單進入活動:
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:錯誤/ AndroidRuntime(352):java.lang.RuntimeException:無法啓動活動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:240 1) 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.Looper.loop(Looper.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(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: ERROR/AndroidRuntime(352):在 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();
這也是我敢肯定,許多第一的錯誤吧會檢測到,因爲我不認爲我正在將課程正確地「膨脹」到活動中以設置選項卡。