2011-07-16 23 views
0

我正在创建一个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(); 

这也是我敢肯定,许多第一的错误吧会检测到,因为我不认为我正在将课程正确地“膨胀”到活动中以设置选项卡。

回答

0

我正在反思这个(像往常一样)。我最终将TabActivity设置为主要活动,而不是将其设置为子类。 TabHost自动加载您的“链接”活动并且非常容易实现。要查找的其他资源是Fragments,因为它遵循相同的原则,并且从外观上看它可能对应用程序非常有用。

相关问题