gpt4 book ai didi

java - 无法从 EditText 转换为 Button

转载 作者:行者123 更新时间:2023-12-01 12:22:50 25 4
gpt4 key购买 nike

我读过很多关于此的文章,但没有帮助我。我尝试过 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);

注册.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 如果这还不够。

关于java - 无法从 EditText 转换为 Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com