通过Intent导航

时间:2014-06-22 01:20:08

标签: android android-intent

我有两个屏幕,如果我只做一个Intent的声明,应用程序不会坚持,但是当试图进入第二个屏幕时它会“意外地”崩溃

主页> Cadastro =好的 主页> 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

任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:1)

<强>问题:

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

您正在将TextView转换为Button,因此会出现此错误:

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

<强>溶液

将您的视图从ID {1}}的布局(layout.activity_cadastro)更改为按钮,因为bt_termosR.id.bt_termos而不是TextView。< / p>

答案 1 :(得分:0)

在MainActivity类

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

startActivity(ⅰ);

在CadastroActivity类中写

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

startActivity(ⅰ);

在目的地活动中,如果你只想要导航,你就不需要写任何东西