2011-07-16 7 views
0

Je crée un menu TabActivity à utiliser dans mon application. Existe-t-il un moyen de construire le menu dans une classe, puis de le gonfler dans chaque activité dans laquelle je veux l'utiliser?Android: gonfler une classe dans une autre

L'activité que je veux gonfler le menu de l'onglet dans:

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

La mise en page xml TabAcitity que je l'ai déjà gonflé dans l'activité principale:

<?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> 

La classe TabActivity J'ai créé pour mettre en place tous mes onglets:

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

}

Lorsque je tente de lancer l'activité que je reçois l'erreur suivante dans le LogCat:

07-16 15: 41: 18,237: ERREUR/AndroidRuntime (352): gestionnaire Uncaught: fil sortie principale en raison d'une exception non interceptée 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo {com.bluelightuniverse.android.brewmobile/com.bluelightuniverse.android.brewmobile .RecipeGrainActivity}: java.lang.NullPointerException 07-16 15: 41: 18.247: ERREUR/AndroidRuntime (352): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:240 1) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): à l'adresse 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): à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1794) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): à l'adresse 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 (méthode native) 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. main (ZygoteInit.java:549) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): à dalvik.system.NativeStart.main (méthode native) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352):

causés par: 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): au com.bluelightuniverse.android.brewmobile.RecipeTabs. (RecipeTabs.java:17) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): au com.bluelightuniverse.android.brewmobile.RecipeGrainActivity.setupView (RecipeGrainActivity.java:20) 07-16 15: 41: 18.247: ERROR/AndroidRuntime (352): au 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)

il me dit que j'ai un problème avec la ligne suivante dans la classe TabActivity:

Resources res = getResources(); 

ce qui est je suis sûr, la première des nombreuses erreurs qu'il détecterait parce que je ne pense pas que je "gonfle" la classe correctement dans l'activité afin de mettre en place les onglets.

Répondre

0

Je réfléchissais dessus (comme d'habitude). J'ai fini par définir TabActivity comme l'activité principale plutôt que d'essayer de la définir comme une sous-classe. TabHost charge vos activités "liées" automatiquement et il est très facile à implémenter. D'autres ressources à rechercher sont des Fragments car ils suivent le même principe et peuvent sembler très utiles dans les applications.

Questions connexes