Navigate by Intent

I have two screens, if I do just a statement of Intent, the App does not stick, however when trying to go to second screen it crashes "unexpectedly"

Home > Cadastro = ok Home > Cadastro > Termos de Uso = Erro

MainActivity.java

package br.com.tilowr.acerteiro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button bt_cadastrese;

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

        bt_cadastrese = (Button) findViewById(R.id.bt_cadastrese);

        bt_cadastrese.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoCadastro();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void gotoCadastro(){
        startActivity(new Intent(this, CadastroActivity.class));
    }

}

CadastroActivity.java

package br.com.tilowr.acerteiro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class CadastroActivity extends Activity {
    private Button bt_termos;

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

        bt_termos = (Button) findViewById(R.id.bt_termos);

        bt_termos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoTermos();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cadastro, menu);
        return true;
    }

    private void gotoTermos(){
        startActivity(new Intent(this, TermosActivity.class));
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.tilowr.acerteiro"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="br.com.tilowr.acerteiro.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="br.com.tilowr.acerteiro.CadastroActivity"
            android:label="@string/title_activity_cadastro" >
        </activity>
        <activity
            android:name="br.com.tilowr.acerteiro.TermosActivity"
            android:label="@string/title_activity_termos" >
        </activity>
    </application>

</manifest>

LogCat Erro:

01-03 05:07:32.550: I/Process(2685): Sending signal. PID: 2685 SIG: 9
01-03 05:07:40.351: D/AndroidRuntime(2729): Shutting down VM
01-03 05:07:40.351: W/dalvikvm(2729): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-03 05:07:40.355: E/AndroidRuntime(2729): FATAL EXCEPTION: main
01-03 05:07:40.355: E/AndroidRuntime(2729): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.tilowr.acerteiro/br.com.tilowr.acerteiro.CadastroActivity}: java.lang.ClassCastException: android.widget.TextView
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.os.Looper.loop(Looper.java:123)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at java.lang.reflect.Method.invoke(Method.java:507)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at dalvik.system.NativeStart.main(Native Method)
01-03 05:07:40.355: E/AndroidRuntime(2729): Caused by: java.lang.ClassCastException: android.widget.TextView
01-03 05:07:40.355: E/AndroidRuntime(2729):     at br.com.tilowr.acerteiro.CadastroActivity.onCreate(CadastroActivity.java:18)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 05:07:40.355: E/AndroidRuntime(2729):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-03 05:07:40.355: E/AndroidRuntime(2729):     ... 11 more

anybody can help?

Upvotes: 0

Views: 84

Answers (2)

Hitesh
Hitesh

Reputation: 66

In MainActivity class

Intent i = new Intent(MainActivity.this, CadastroActivity.class);

startActivity(i);

In CadastroActivity class write

Intent i = new Intent(CadastroActivity.this, Termos.class);

startActivity(i);

in destination activity you can`t need to write anything if you just want navigate

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

problem:

bt_termos = (Button) findViewById(R.id.bt_termos);

You are casting a TextView to Button thus giving you this error:

Caused by: java.lang.ClassCastException: android.widget.TextView

solution:

change your View from the layout(layout.activity_cadastro) with the id of bt_termos to button, because R.id.bt_termos is a TextView not a Button.

Upvotes: 1

Related Questions