The app stops when you want to open another activity

1

When trying to open another activity the App stops, I have a activity with a TabsHost and I try to open another one with a TabHost . I leave the code, I hope you can help me:

The java of 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();
}

XML of 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>

the other XML is practically the same and the java of that 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);
}
    
asked by Shait Carrillo 28.05.2017 в 06:48
source

1 answer

0

I have reproduced your code and I tell you what your problems may be, since you say you can not see the Logcat .

1 . Look at your AndroidManifest if you have declared both clases , you should have something like this:

<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 . In your class MainActivity you declare as id

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

but in your main you declare it as Tabs . Very important, check that you have the same id in the class as layout .

3 . I'll give you an example of how you should have MainPage and activity_main_page , what is where you comment that you close.

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>

    
answered by 28.05.2017 / 22:29
source