进入下一页时应用程序崩溃(意图)

时间:2013-11-26 00:11:07

标签: java android eclipse android-intent logcat

来自Logcat:

11-25 18:49:03.244: D/gralloc_goldfish(1269): Emulator without GPU emulation detected.
11-25 18:49:13.403: D/AndroidRuntime(1269): Shutting down VM
11-25 18:49:13.403: W/dalvikvm(1269): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-25 18:49:13.643: D/dalvikvm(1269): GC_FOR_ALLOC freed 97K, 7% free 2921K/3136K, paused 65ms, total 84ms
11-25 18:49:13.643: E/AndroidRuntime(1269): FATAL EXCEPTION: main
11-25 18:49:13.643: E/AndroidRuntime(1269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlassignmentb/com.example.sqlassignmentb.SignUpActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.os.Looper.loop(Looper.java:137)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at java.lang.reflect.Method.invoke(Method.java:525)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at dalvik.system.NativeStart.main(Native Method)
11-25 18:49:13.643: E/AndroidRuntime(1269): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-25 18:49:13.643: E/AndroidRuntime(1269):     at com.example.sqlassignmentb.SignUpActivity.onCreate(SignUpActivity.java:27)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.Activity.performCreate(Activity.java:5133)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-25 18:49:13.643: E/AndroidRuntime(1269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-25 18:49:13.643: E/AndroidRuntime(1269):     ... 11 more

LoginActivity.java - 现在几乎什么都没做,除了它应该带我到注册页面

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

public class LoginActivity extends Activity implements OnClickListener {

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

    Button signUpButton = (Button) findViewById(R.id.buttonSignUp);
    signUpButton.setOnClickListener(this);
}

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

@Override
public void onClick(View arg0) {
    Intent signupPage = new Intent(LoginActivity.this, SignUpActivity.class);
    startActivity(signupPage); 

}

}

最后是注册页面。

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SignUpActivity extends LoginActivity implements OnClickListener {
Button back;
Button create;
TextView message;

EditText username;
EditText firstname;
EditText lastname;
EditText password;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.signup);
    // Text Fields
    username = (EditText) findViewById(R.id.textViewSignUpUsername);
    firstname = (EditText) findViewById(R.id.editTextSignUpFirstName);
    lastname = (EditText) findViewById(R.id.editTextSignUpLastName);
    password = (EditText) findViewById(R.id.editTextSignUpUsername);
    // Buttons
    back = (Button) findViewById(R.id.buttonBack);
    create = (Button) findViewById(R.id.buttonSignUpButtonCreate);
    // Message to display if user is created or not
    message = (TextView) findViewById(R.id.textViewMessage);
    // Button listeners
    back.setOnClickListener(this);
    create.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.buttonBack:
        Intent homepage = new Intent(SignUpActivity.this,
                LoginActivity.class);
        startActivity(homepage);

        break;
    case R.id.buttonSignUpButtonCreate:
        if (username.getText().length() == 0
                || firstname.getText().length() == 0
                || lastname.getText().length() == 0
                || password.getText().length() == 0) {
            message.setText("Error. User not created. Please make sure all fields are filled in. ");
        } else {

            message.setText("User Created.");
        }

        break;

    }
}


}

从log cat中可以理解,它表明它混合/混淆了TextView和EditText的东西

1 个答案:

答案 0 :(得分:1)

根据你的命名惯例,我认为这一行是罪魁祸首:

username = (EditText) findViewById(R.id.textViewSignUpUsername);

我认为textViewSignUpUsername实际上是TextView,而不是EditText