**问题:**我在Android Studio中有一个java/xml文件。我需要弹两个按钮;一个用于“Map页面”,另一个用于“创建新帐户”(注册活动)。第一个按钮工作,但第二个关闭我的应用程序.有什么问题吗?
**我所知道的:**如果它是空的xml文件,则转到xml文件的按钮工作。
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sampleapp, PID: 8062
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampleapp/com.example.sampleapp.SignUpActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at com.example.sampleapp.SignUpActivity.onCreate(SignUpActivity.java:33)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
字符串
脚本:
- 登录活动.java:*
package com.example.sampleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
public EditText username;
public EditText password;
public TextView errorView;
public Button login;
public Button createNewAccount;
public void initMapPage(){
Intent mapPage = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mapPage);
finish();
}
public void initSignUpPage(){
Intent signUpPage = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(signUpPage);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.UsernameBox);
password = (EditText) findViewById(R.id.PasswordBox);
errorView = (TextView) findViewById(R.id.errorView);
login = (Button) findViewById(R.id.LoginButton);
createNewAccount = (Button) findViewById(R.id.SignupButton);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String errorMsg = "";
if (username.getText().toString().equals("") || password.getText().toString().equals("")) {
if (username.getText().toString().equals("")) {
errorMsg += "Username block is empty\n";
}
if (password.getText().toString().equals("")) {
errorMsg += "Password block is empty\n";
}
errorView.setText(errorMsg);
}
else {
initMapPage();
}
}
});
createNewAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initSignUpPage();
}
});
}
}
型
- 活动登录.xml:*
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<TextView
android:id="@+id/WelcomeBox"
android:layout_width="210dp"
android:layout_height="101dp"
android:layout_marginTop="64dp"
android:text="Welcome \nTo Our App!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/UsernameBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/WelcomeBox" />
<EditText
android:id="@+id/PasswordBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/UsernameBox" />
<Button
android:id="@+id/SignupButton"
android:layout_width="237dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:text="Create New Acount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<Button
android:id="@+id/LoginButton"
android:layout_width="115dp"
android:layout_height="63dp"
android:layout_marginTop="12dp"
android:text="LOGIN"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/errorView" />
<TextView
android:id="@+id/textView4"
android:layout_width="60dp"
android:layout_height="29dp"
android:layout_marginTop="24dp"
android:text="OR"
android:textAlignment="center"
android:textColor="#D3000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LoginButton" />
<TextView
android:id="@+id/errorView"
android:layout_width="209dp"
android:layout_height="62dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:textAlignment="center"
android:textColor="#FF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PasswordBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
型
- 注册活动.java:*
package com.example.sampleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SignUpActivity extends AppCompatActivity {
public Button backToLogin;
public Button SignUp;
public EditText username;
public EditText password;
public EditText email;
public void initLoginPage(){
Intent loginPage = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(loginPage);
finish();
}
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
SignUp = (Button) findViewById(R.id.suButton);
backToLogin = (Button) findViewById(R.id.back_to_login);
username = (EditText) findViewById(R.id.UsernameBoxSignUp);
password = (EditText) findViewById(R.id.PasswordBoxSignUp);
email = (EditText) findViewById(R.id.EmailBox);
backToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initLoginPage();
}
});
}
}
型
- 活动注册.xml:*
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SignUpActivity">
<EditText
android:id="@+id/UsernameBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="54dp"
android:layout_marginTop="160dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/back_to_login" />
<EditText
android:id="@+id/PasswordBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="55dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/EmailBox" />
<Button
android:id="@+id/suButton"
android:layout_width="170dp"
android:layout_height="53dp"
android:layout_marginStart="120dp"
android:layout_marginTop="36dp"
android:text="SignUp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PasswordBoxSignUp" />
<EditText
android:id="@+id/EmailBox"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="44dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/UsernameBoxSignUp" />
<ImageButton
android:id="@+id/back_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="54dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/actionModeCloseDrawable" />
</androidx.constraintlayout.widget.ConstraintLayout>
型
4条答案
按热度按时间nkoocmlb1#
问题是
ImageButton
(以及appcompat
version)是ImageView
的子类,而不是Button
的子类。如果只使用View
方法,例如setOnClickListener
,则应该将强制转换更改为该类型,或者将其完全移除。ee7vknir2#
是否设置了用户名或密码文本?
Android:text="”提示只是一个提示,而不是实际的响应。所以当它查找还没有设置好的东西时会出错。
vu8f3i0k3#
您的呼叫SignUpActivity两次
放置意图
字符串
onClick方法
别忘了在按钮上添加
android:onClick="OnCreateAccount"
olhwl3o24#
您正在图像按钮上铸造按钮
只要按照这个简单的步骤
转到“SignUpActivity.java”,然后
字符串