Android Studio按钮进入错误页面



在我的主页中,我有3个不同的按钮。

  1. 登录(loginB(
  2. 创建新帐户(createB(
  3. 行为条款

到目前为止,它运行得很好,但由于某种原因,我现在遇到了创建新帐户按钮的问题。它以前确实转到了正确的页面,但现在转到了错误的页面。它应该转到CreateAccount页面,但它要转到Menu页面。

我在MainActivity中有java代码,按钮在这里:

public class MainActivity extends AppCompatActivity {
Button loginB;
Button termsB;
Button createB;
EditText emailL,passwordL;
FirebaseAuth fAuth;
ProgressBar progressB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createB = findViewById(R.id.createB);
createB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCreateAccount();
}
});
termsB = findViewById(R.id.termsB);
termsB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openTermsOfConduct();
}
});
emailL = findViewById(R.id.emailL);
passwordL = findViewById(R.id.passwordL);
fAuth = FirebaseAuth.getInstance();
loginB = findViewById(R.id.loginB);
progressB = findViewById(R.id.progressB);
loginB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailL.getText().toString().trim();
String password = passwordL.getText().toString().trim();
if(TextUtils.isEmpty(email)){
emailL.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(password)){
passwordL.setError("Password is required.");
return;
}
if(password.length() <6){
passwordL.setError("Password must be at least 6 characters.");
}
progressB.setVisibility(View.VISIBLE);
//authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Menu.class));
}
else{
Toast.makeText(MainActivity.this, "Error " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressB.setVisibility(View.INVISIBLE);
}
}
});
}
});
}
public void openCreateAccount(){
Intent intent = new Intent(this, CreateAccount.class);
startActivity(intent);
}
public void openTermsOfConduct(){
Intent intent = new Intent(this, TermsOfConduct.class);
startActivity(intent);
}
}

主要活动xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@drawable/tausta">
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:fontFamily="sans-serif"
android:paddingVertical="30dp"
android:text="Login"
android:textAlignment="center"
android:textColor="#000"
android:textSize="90sp"
android:layout_marginBottom="25dp"
/>
<EditText
android:id="@+id/emailL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="30dp"
android:textColor="#fff"
android:textSize="25sp"
android:hint="Email"
android:textColorHint="#fff"
/>
<EditText
android:id="@+id/passwordL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:inputType="textPassword"
android:layout_marginBottom="30dp"
android:textColor="#fff"
android:textSize="25sp"
android:hint="Password"
android:textColorHint="#fff"
/>
<Button
android:id="@+id/loginB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Login"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="@drawable/button_bg"
/>
<Button
android:id="@+id/createB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Create a new account"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="@drawable/button_bg"
/>
<Button
android:id="@+id/termsB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Terms and conditions"
android:layout_marginHorizontal="30dp"
android:textColor="#0070ff"
android:textAlignment="center"
android:background="@android:color/transparent"
/>
<ProgressBar
android:id="@+id/progressB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>

</LinearLayout>

CreateAccount java:

public class CreateAccount extends AppCompatActivity {
Button registerB;
EditText nameR,emailR,pnumberR,passwordR;
FirebaseAuth fAuth;
ProgressBar progressB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account2);
nameR = findViewById(R.id.nameR);
emailR = findViewById(R.id.emailR);
pnumberR = findViewById(R.id.pnumberR);
passwordR = findViewById(R.id.passwordR);
progressB = findViewById(R.id.progressB);
fAuth = FirebaseAuth.getInstance();
//check if user registered
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),Menu.class));
finish();
}

registerB = findViewById(R.id.registerB);
registerB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailR.getText().toString().trim();
String password = passwordR.getText().toString().trim();
if(TextUtils.isEmpty(email)){
emailR.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(password)){
passwordR.setError("Password is required.");
return;
}
if(password.length() <6){
passwordR.setError("Password must be ata least 6 characters.");
return;
}
progressB.setVisibility(View.VISIBLE);
//register the user to FireBase
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(CreateAccount.this, "User created.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Menu.class));
}
else{
Toast.makeText(CreateAccount.this, "Error " +task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}

CreateAccount xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CreateAccount"
android:background="@drawable/tausta"
>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:fontFamily="sans-serif"
android:paddingVertical="20dp"
android:text="Create new account"
android:textAlignment="center"
android:textColor="#000"
android:textSize="40sp"
android:layout_marginBottom="25dp"
/>
<EditText
android:id="@+id/nameR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full name"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="@+id/emailR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="@+id/pnumberR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>

<EditText
android:id="@+id/passwordR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#fff"
android:textSize="25sp"
android:layout_marginHorizontal="25dp"
android:layout_marginBottom="30dp"
/>
<Button
android:id="@+id/registerB"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Register"
android:layout_marginHorizontal="30dp"
android:layout_marginBottom="25dp"
android:background="@drawable/button_bg"
/>
<ProgressBar
android:id="@+id/progressB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</LinearLayout>

我想您肯定应该检查(R.layout.activity_create_account2),以及它是否是CreateAccount.java 的正确xml

附言:在发布问题时,也要提到xml的正确名称。

以下代码可能是原因:

if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),Menu.class));
finish();
}

您能检查它是否进入if块吗?

最新更新