0

アプリケーション全体で使用する TabActivity メニューを作成しています。1 つのクラスでメニューを構築し、それを使用したい各アクティビティに拡張する方法はありますか?

タブメニューを膨らませたいアクティビティ:

    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);
}

メイン アクティビティに既に膨らませた TabAcity 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: エラー/AndroidRuntime(352): キャッチされていないハンドラー: キャッチされていない例外が原因でスレッド メインが終了するアクティビティ 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: ERROR/AndroidRuntime(352): 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();

タブを設定するためにクラスをアクティビティに正しく「インフレート」しているとは思わないため、検出される多くのエラーの最初のものだと確信しています。

4

1 に答える 1

0

私はこれを考えすぎていました(いつものように)。サブクラスとして設定しようとするのではなく、TabActivity をメイン アクティビティとして設定することになりました。TabHost は、「リンクされた」アクティビティを自動的にロードし、実装が非常に簡単です。参照する他のリソースは Fragments です。これは、同じ原則に従い、その外観からアプリケーションで非常に役立つ可能性があるためです。

于 2011-07-17T06:49:44.623 に答える