1

Al intentar abrir otra activity se detiene la App, tengo un activity con un TabsHost e intento abrir otro con un TabHost. Les dejo el código, espero me puedan ayudar:

El java del MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabs= (TabHost) findViewById(R.id.Tabs2);
    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("Login");
    spec.setIndicator("Login");
    spec.setContent(R.id.Login);
    tabs.addTab(spec);

    spec=tabs.newTabSpec("registro");
    spec.setIndicator("Registrase");
    spec.setContent(R.id.Registrase);
    tabs.addTab(spec);

    tabs.setCurrentTab(0);
}

public void evtLogIn(View view) {
    Intent intent = new Intent(this, MainPage.class);
    startActivity(intent);
    finish();
}

El XML de Main:

<TabHost
    android:id="@+id/Tabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/Login"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <EditText
                    android:layout_width="250dp"
                    android:hint="Nombre de usuario"
                    android:layout_height="wrap_content" />

                <EditText
                    android:layout_width="250dp"
                    android:hint="Contraseña"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:text="Ingresar"
                    android:onClick="evtLogIn"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/Registrase"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <EditText
                    android:layout_width="250dp"
                    android:hint="Nombre de usuario"
                    android:layout_height="wrap_content" />

                <EditText
                    android:layout_width="250dp"
                    android:hint="Contraseña"
                    android:layout_height="wrap_content" />

                <EditText
                    android:layout_width="250dp"
                    android:hint="Confirmar contraseña"
                    android:layout_height="wrap_content" />

                <Button
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:text="Registrarse"
                    />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

el otro XML es prácticamente lo mismo y el java de ese Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);

    TabHost tabs= (TabHost) findViewById(R.id.Tabs);
    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("Publicaciones");
    spec.setIndicator("Publicaciones");
    spec.setContent(R.id.Publicaciones);
    tabs.addTab(spec);

    spec=tabs.newTabSpec("Publica");
    spec.setIndicator("Publica");
    spec.setContent(R.id.Publicar);
    tabs.addTab(spec);

    tabs.setCurrentTab(0);
}
4
  • Qué error muestra? en el "log"
    – Dev. Joel
    el 28 may. 2017 a las 4:49
  • me es imposible saberlo, android studio no me reconoce ni el celular en modo debug ni el emulador que tengo instalado el 28 may. 2017 a las 4:52
  • podrías empezar por obtener los logs. Intenta resolver el problema del IDE o usa adb por linea de comandos developer.android.com/studio/command-line/adb.html?hl=es-419 el 28 may. 2017 a las 5:13
  • ¿Has declarado MainPage en tu manifest? Pero como dicen, colocar el Logcat seria de gran ayuda.
    – Israel
    el 28 may. 2017 a las 8:33

1 respuesta 1

0

He reproducido tu código y te comento lo que pueden ser tus problemas, ya que comentas que no puedes ver el Logcat.

1. Mira en tu AndroidManifest si has declarado ambas clases, deberías tener algo así:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainPage"></activity>

2. En tu clase MainActivity declaras como id

TabHost tabs= (TabHost) findViewById(R.id.Tabs2);

pero en tu main lo declaras como Tabs. Muy importante eso, comprueba que tienes la misma id en la clase como layout.

3. Te dejo un ejemplo de como deberías tenerMainPage y activity_main_page, qué es donde comentas que se te cierra.

MainPage:

public class MainPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        TabHost tabs = (TabHost) findViewById(R.id.Tabs2);
        tabs.setup();

        TabHost.TabSpec spec = tabs.newTabSpec("Publicaciones");
        spec.setIndicator("Publicaciones");
        spec.setContent(R.id.Publicaciones);
        tabs.addTab(spec);

        spec = tabs.newTabSpec("Publica");
        spec.setIndicator("Publica");
        spec.setContent(R.id.Publicar);
        tabs.addTab(spec);

        tabs.setCurrentTab(0);
    }
}

activity_main_page

<TabHost
    android:id="@+id/Tabs2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/Publicaciones"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Hola"
                    android:textAlignment="center"
                    android:textSize="30sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/Publicar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

1
  • Me has salvado hermano muchas gracias :D el 28 may. 2017 a las 23:03

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.