我已经阅读了很多有关此的文章,但并没有帮助我。我已经尝试过clean
和re-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').
最佳答案
我想您将两个按钮声明为TextFields
,而不是Buttons
。这就是为什么它说项目有错误,甚至无法运行。如果它运行后抛出错误,导致无法转换,那就是问题了。
我想您会复制粘贴控件声明,而忘记更改其类型。
更新
为什么要覆盖findViewById
方法?摆脱它。如您所见,您正在覆盖将返回正确对象的默认方法。您的方法返回错误的对象(TextField
)-这就是为什么它说不能将其从TextField
强制转换为Button
的原因,如果还不够,则为null
。