Android:将一个类膨胀为另一个类
我正在创建一个 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 布局:
<线性布局 android:方向=“垂直” 安卓:layout_width =“fill_parent” 安卓:layout_height =“fill_parent” android:padding="5dp"> android:id="@android:id/tabs" 安卓:layout_width =“fill_parent” android:layout_height="wrap_content" /> <框架布局 android:id="@android:id/tabcontent" 安卓:layout_width =“fill_parent” 安卓:layout_height =“fill_parent” android:padding="5dp" //>
我创建的 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: 错误/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:错误/AndroidRuntime(352):在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:错误/AndroidRuntime(352):在java.lang.reflect.Method.invokeNative(本机方法) 07-16 15:41:18.247:错误/AndroidRuntime(352):在java.lang.reflect.Method.invoke(Method.java:521) 07-16 15:41:18.247: 错误/AndroidRuntime(352): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 07-16 15:41:18.247:错误/AndroidRuntime(352):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 07-16 15:41:18.247:错误/AndroidRuntime(352):在dalvik.system.NativeStart.main(本机方法) 07-16 15:41:18.247: 错误/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: 错误/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();
我确信,它会检测到的许多错误中的第一个,因为我没有我认为我正在将类正确地“膨胀”到活动中以设置选项卡。
I am creating a TabActivity menu to use throughout my application. Is there a way to build the menu in one class, and then inflate it into each activity I want to use it in?
The activity I want to inflate the tab menu into:
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);
}
The TabAcitity xml layout that I have already inflated into the main activity:
<?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>
The TabActivity class I have created to set up all my tabs:
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);
}
}
When I try to run the activity I get the following error in the LogCat:
07-16 15:41:18.237: ERROR/AndroidRuntime(352): Uncaught handler: thread main exiting due to uncaught exception
07-16 15:41:18.247: ERROR/AndroidRuntime(352): java.lang.RuntimeException: Unable to start activity 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: 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):Caused by: 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): 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)
It's telling me I have an issue with the following line in the TabActivity class:
Resources res = getResources();
Which is i'm sure, the first of many errors it would detect because I don't think I am "inflating" the class correctly into the activity in order to set up the tabs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想太多了(像往常一样)。我最终将 TabActivity 设置为主要活动,而不是尝试将其设置为子类。 TabHost 自动加载您的“链接”活动,并且非常容易实现。其他要查找的资源是片段,因为它遵循相同的原理,并且从它的外观来看,它在应用程序中非常有用。
I was overthinking this (as usual). I ended up setting the TabActivity as the main activity rather than trying to set it as a subclass. TabHost loads your "linked" activities automatically and its very easy to implement. Other resources to look up are Fragments as it follows the same principle and from the looks of it can be very useful in applications.