java - 转到下一页时应用程序崩溃( Intent )

标签 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 与某些内容混合/混淆

最佳答案

根据您的命名约定,我认为这一行是罪魁祸首:

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

我想 textViewSignUpUsername 实际上是一个 TextView,而不是 EditText

关于java - 转到下一页时应用程序崩溃( Intent ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206121/

相关文章:

java - RCP - 如何用另一个图像装饰图像

java - 在 Java 字节码/类格式中,什么决定一个方法是否覆盖另一个方法?

android - 如何使用 DevicePolicyManager 解锁手机

android - 在 Sqlite 中将位图存储为 BLOB 是个好主意吗?

android - FAILURE 构建失败,出现异常 cordova。当构建命令运行时

java - 嵌套对象空字段的 Spring 表单绑定(bind)问题

java - 如何设置 JDI 启动连接器?

c++ - 在此示例中,在对类的引用之前使用 * 运算符是什么意思

Eclipse:单击 "Open Perspective"时如何配置菜单上列出的透视图?

java: NullPointerException,需要帮助