Problema con Tab App

di il
8 risposte

Problema con Tab App

Salve, sono nuovo, spero che riuscirò a trovare una risposta e magari anche una soluzione ad un mio problema. Sto sviluppando un app per android; per ora mi interessa solo creare l'interfaccia e nello specifico fare un'applicazione con 2 tab. Solo che ogni volta che apro l'app crasha. Il codice è questo:
package com.tecnogroup.cosametto;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


public class CosaIndosso extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cosa_indosso);
        TabHost tabhost = (TabHost) findViewById(R.id.tabhost);
        tabhost.setup();
        
        TabSpec spec1 = tabhost.newTabSpec("Tab1");
        spec1.setContent(R.layout.foto);
        Intent foto = new Intent(CosaIndosso.this, foto.class);
        spec1.setContent(foto);
        spec1.setIndicator("Tab1");
        
        TabSpec spec2 = tabhost.newTabSpec("Tab2");
        
        spec2.setIndicator("Tab2");
        
        tabhost.addTab(spec1);
        tabhost.addTab(spec2);
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_cosa_indosso, menu);
        return true;
    }
}
Mentre il logcat è questo:
11-07 14:16:21.102: D/AndroidRuntime(364): Shutting down VM
11-07 14:16:21.112: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-07 14:16:21.122: E/AndroidRuntime(364): FATAL EXCEPTION: main
11-07 14:16:21.122: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecnogroup.cosametto/com.tecnogroup.cosametto.CosaIndosso}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2130903041
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.os.Looper.loop(Looper.java:123)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at java.lang.reflect.Method.invoke(Method.java:507)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at dalvik.system.NativeStart.main(Native Method)
11-07 14:16:21.122: E/AndroidRuntime(364): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2130903041
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:593)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:584)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.widget.TabHost$TabSpec.setContent(TabHost.java:441)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at com.tecnogroup.cosametto.CosaIndosso.onCreate(CosaIndosso.java:21)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-07 14:16:21.122: E/AndroidRuntime(364): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-07 14:16:21.122: E/AndroidRuntime(364): 	... 11 more
Vi prego di aiutarmi, grazie

8 Risposte

  • Re: Problema con Tab App

    Ciao! Per quanto riguarda la gestione tramite tab, dalla versione Honeycomb in poi (quindi API level >= 11) si utilizzano i Fragment & ActionBar (personalmente trovo la gestione più complicata rispetto a come si faceva prima).

    Detto questo, l'errore che ti viene dato è che non riconosce l'id del tuo tabhost. Io ho sviluppato qualche applicazione con i tab (per API level < 11) e come ID per il TabHost bisognava mettere "@android:id/tabhost" mentre per il tabwidget serve "@android:id/tabs", quindi immagino che nel tuo "findviewbyid" tu debba mettere android.R.id.tabhost invece dell'id che specifichi tu.
    Inoltre esiste una specializzazione di Activity detta TabActivity che ti permette di gestire in maniera più semplice i tab (ora è, come detto, deprecata poiché la nuova gestione prevede i Fragment), ad esempio ti permette di ottenere il TabHost attraverso il metodo getTabHost().
  • Re: Problema con Tab App

    Ho provato a fare come mi hai suggerito, ma quando metto come id android:id/tabhost, quando faccio il findViewById(R.id.tabhost) non mi trova tabhost.
  • Re: Problema con Tab App

    Questo è il file del layout
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TabHost 
            android:id="@android:id/tabhost"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >
        
    </TabWidget>
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@android:id/tabcontent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            tools:context=".CosaIndosso" 
            />
        </FrameLayout>
    </TabHost>
    
    
    
    
    </RelativeLayout>
    
    
  • Re: Problema con Tab App

    E' giusto l'errore, infatti usi un id che è definto da android, non da te. Prova a mettere android.R.id.tabhost

    EDIT: devi anche chiamare il metodo setup() sul tabhost prima di poter aggiungere dei tab!
  • Re: Problema con Tab App

    Niente ancora, ho sistemato così il codice eppure niente ancora:
    package com.example.cosaindosso;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.TabActivity;
    import android.view.Menu;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    
    public class CosaIndosso extends TabActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cosa_indosso);
            TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
            tab.setup();
            
            TabSpec spec1 = tab.newTabSpec("tab1");
            spec1.setIndicator("Foto");
            
            TabSpec spec2 = tab.newTabSpec("tab2");
            spec2.setIndicator("Random");
            
            tab.addTab(spec1);
            tab.addTab(spec2);
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_cosa_indosso, menu);
            return true;
        }
    }
    11-10 16:36:11.904: D/szipinf(10937): Initializing inflate state
    11-10 16:36:12.234: D/dalvikvm(10937): GC_EXTERNAL_ALLOC freed 53K, 51% free 2675K/5379K, external 0K/0K, paused 179ms
    11-10 16:36:12.254: D/AndroidRuntime(10937): Shutting down VM
    11-10 16:36:12.254: W/dalvikvm(10937): threadid=1: thread exiting with uncaught exception (group=0x40018560)
    11-10 16:36:12.254: E/AndroidRuntime(10937): FATAL EXCEPTION: main
    11-10 16:36:12.254: E/AndroidRuntime(10937): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cosaindosso/com.example.cosaindosso.CosaIndosso}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.os.Looper.loop(Looper.java:130)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread.main(ActivityThread.java:3835)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at java.lang.reflect.Method.invokeNative(Native Method)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at java.lang.reflect.Method.invoke(Method.java:507)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at dalvik.system.NativeStart.main(Native Method)
    11-10 16:36:12.254: E/AndroidRuntime(10937): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.widget.TabHost.addTab(TabHost.java:202)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at com.example.cosaindosso.CosaIndosso.onCreate(CosaIndosso.java:25)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
    11-10 16:36:12.254: E/AndroidRuntime(10937): 	... 11 more
  • Re: Problema con Tab App

    Se usi una TabActivity puoi ottenere il riferimento al TabHost tramite
    
    TabHost tab = getTabHost();
    
    EDIT: riguardandolo, penso che il problema sia dovuto al fatto che non hai impostato un contenuto per i vari tab.
    prova a guardare qua http://www.androidpeople.com/android-tabhost-tutorial-part-1
  • Re: Problema con Tab App

    Ok, grazie al link che mi hai dato ho risolto, forse era solo un problema di contenuto che avevo mancato. Grazie per l'aiuto
  • Re: Problema con Tab App

    Di niente! Comunque credo che si, il problema fosse la mancanza di contenuto
Devi accedere o registrarti per scrivere nel forum
8 risposte