2011-07-16 9 views
0

私はアプリケーション全体で使用するTabActivityメニューを作成しています。 1つのクラスでメニューを作成し、それを使用したい各アクティビティに展開する方法はありますか?Android: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); 
} 

私はすでに主な活動に膨張している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で次のエラーを取得する:

7月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.NullPointerException 07-16 15:41:18.247:ERROR/AndroidRuntime(352):android.app.ActivityThread.performLaunchActivity(ActivityThread.java:240) 1) 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):java.lang.reflect.Method.invokeNative(ネイティブメソッド) 07-16 15:41:18.247:ERROR/AndroidRuntime(352):java.lang.reflect.Method.invoke(Method.java:521) 07-16 15:41:18.247:ERROR/AndroidRuntime(352) :com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791) 07-16 15:41:18.247:ERROR/AndroidRuntime(352):com.android.internal.os.ZygoteInit。メイン(ZygoteInit.java:549) 07-16 15:41:18.247:ERROR/AndroidRuntime(352):at dalvik.system.NativeStart.main(ネイティブメソッド) 07-16 15:41:18.247:ERROR/AndroidRuntime java.lang.NullPointerExceptionが07から16:15:41:18.247:(352)によって発生

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:ERROR/AndroidRuntime 352): com.bluelightuniverse.android.brewmobile.RecipeGrainActivity.setupView(RecipeGrainActivity.java:20) 07-16 15:41:18.247:ERROR/AndroidRuntime(352): com.bluelightuniverse.android.brewmobile。 RecipeGrainActivity.onCreate(RecipeGrainActivity.java:15) 07-16 15:41:18。247:ERROR/AndroidRuntime(352): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 07-16 15:41:18.247:ERROR/AndroidRuntime(352): android.app.ActivityThread。 performLaunchActivity(ActivityThread.java:2364)

それは私がTabActivityクラスに次の行に問題が持っている私に言っています:それは多くのエラーの最初の、

Resources res = getResources(); 

私は確信していますタブを設定するために、クラスをアクティビティに正しく「入れ替える」とは思わないので、検出されます。

答えて

0

私はこれを(いつものように)考え直していました。 TabActivityをサブクラスとして設定するのではなく、主なアクティビティとして設定しました。 TabHostはリンクされたアクティビティを自動的に読み込み、実装するのが非常に簡単です。ルックアップする他のリソースは、同じ原則に従うフラグメントであり、外観からはアプリケーションで非常に役立ちます。

関連する問題