无法从EditText转换为Button

时间:2014-10-24 14:08:39

标签: java android

我已经就此发了很多帖子,但没有帮助我。我尝试了cleanre-build项目,但也没有帮助我。我真的不明白这个错误是什么意思。有人可以帮忙解决这个错误吗?

RegisterActivity.java

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import library.DatabaseHandler;
import library.UserFunctions;

public class RegisterActivity extends Activity {
Button buttonRegister;
Button buttonLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    // Importing all assets like buttons, text fields
    inputFullName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    buttonRegister = (Button) findViewById(R.id.buttonRegister);
    buttonLinkToLogin = (Button) findViewById(R.id.buttonLinkToLoginScreen);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);

    // Register Button Click event
    buttonRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            ProgressDialog progressDialog = new ProgressDialog(RegisterActivity.this);
            progressDialog.setMessage("Registering...");
            RegisterTask registerTask = new RegisterTask(RegisterActivity.this, progressDialog);
            registerTask.execute();
        }
    });

    // Link to Login Screen
    buttonLinkToLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(i);
            // Close Registration View
            finish();
        }
    });
}

public void registerReport(Integer responseCode) {
    int duration = Toast.LENGTH_LONG;
    Context context = getApplicationContext();

    if (responseCode == 0) {
        Toast toast = Toast.makeText(context, "Register Error", duration);
        toast.show();
    }
    else {
        Toast toast = Toast.makeText(context, "Register Success", duration);
        toast.show();
        Intent i = new Intent(getApplicationContext(),
                DashboardActivity.class);
        startActivity(i);
        finish();
    }



}

public EditText findViewById(int registeremail) {
    // TODO Auto-generated method stub
    return null;
}
}

线条就是这个

buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLinkToLogin = (Button) findViewById(R.id.buttonLinkToLoginScreen);

Register.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3b3b3b" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >
    <!--  View Title Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="REGISTER"
        android:textSize="25dip"
        android:textStyle="bold" />
    <!--  Name Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name" />
    <!--  Name TextField -->
    <EditText
        android:id="@+id/registerName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:singleLine="true"/>

    <!--  Email Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Email" />
    <!--  Email TextField -->
    <EditText
        android:id="@+id/registerEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:singleLine="true"/>

    <!--  Password Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="Password" />
    <!--  Password TextField -->
    <EditText
        android:id="@+id/registerPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" 
        android:singleLine="true"/>


    <!--  Error message -->
    <TextView android:id="@+id/register_error"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#e30000"
                android:padding="10dip"
                android:textStyle="bold"/>

    <!--  Login Button -->

    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:text="Register" />

    <!--  Link to Login Screen -->
    <Button
        android:id="@+id/buttonLinkToLoginScreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:background="@null"
        android:text="Already registred. Login Me!"
        android:textColor="#21dbd4"
        android:textStyle="bold" />
</LinearLayout>

</ScrollView>

更新

Button buttonRegister;
Button buttonLinkToLogin;

我可以在控制台输出中看到这个

res\layout\register.xml:75: error: Error: No resource found that matches the given name (at 'id' with value '@id/buttonLinkToLoginScreen').
res\layout\register.xml:67: error: Error: No resource found that matches the given name (at 'id' with value '@id/buttonRegister').

2 个答案:

答案 0 :(得分:1)

我想您将两个按钮声明为TextFields,而不是Buttons。这就是为什么它说项目有错误甚至无法运行。如果它运行然后抛出错误,它就无法投射,问题就会出现问题。

我想你复制粘贴的控件声明,忘了改变它们的类型。

<强>更新

为什么要覆盖findViewById方法?摆脱它。正如您所看到的,您将覆盖将返回正确对象的默认方法。您的方法返回错误的对象(TextField) - 这就是为什么它表示无法从TextField投射到Button,如果这还不够,则为null。< / p>

答案 1 :(得分:0)

三种可能的解决方案;

  • 在XML文件中的ID中尝试所有小写字母
  • 重启Eclipse
  • res目录中不应保留任何错误,否则无法识别ID,如果有任何红色标记则清除

这些经常解决这类问题。