gpt4 book ai didi

java - Android StartActivity 不会启动新 Activity

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

我在这个类中定义了两个方法:

RegisterButtonClicked 成功调用 Activity ,但 LoginButtonClicked 不会...

运行此代码的结果是打印出“inside LoginButtonClicked2”日志消息,这意味着代码正在运行 LoginButtonClicked 方法,但未成功执行该方法内的 startActivity。

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void RegisterButtonClicked(View view) {
Log.d("registerButtonClicked", "inside registerButtonClicked");
Button Register = findViewById(R.id.RegisterButton);
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, Register.class));
}
});
}
public void LoginButtonClicked(View view) {
Log.d("LoginButtonClicked", "inside LoginButtonClicked");
Button login = findViewById(R.id.LoginButton);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("LoginButtonClicked", "inside LoginButtonClicked2");
startActivity(new Intent(MainActivity.this, Login2.class));
}
});
}
}

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity">

<Button
android:id="@+id/RegisterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="191dp"
android:layout_marginStart="134dp"
android:layout_marginLeft="134dp"
android:text="Register"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="266dp"
android:onClick = "RegisterButtonClicked"/>
<Button
android:id="@+id/LoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="150dp"
android:layout_marginStart="134dp"
android:layout_marginLeft="134dp"
android:text="Log in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="200dp"
android:onClick = "LoginButtonClicked"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginStart="107dp"
android:layout_marginLeft="107dp"
android:text="Welcome to Pick Rose!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pickrose3, PID: 8252
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pickrose3/com.pickrose3.Login2}: java.lang.IllegalArgumentException: Given String is empty or null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(com.google.firebase:firebase-auth@@19.0.0:202)
at com.pickrose3.Login2.onCreate(Login2.java:60)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

最佳答案

您可以按如下方式更改您的 java 代码(请将其视为解决此问题的可能方法之一!):

public class MainActivity extends AppCompatActivity {

public void RegisterButtonClicked(View RegisterButtonClicked) {
startActivity(new Intent(MainActivity.this, Register.class));
}

public void LoginButtonClicked(View LoginButtonClicked) {
startActivity(new Intent(MainActivity.this, Login2.class));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

}

关于java - Android StartActivity 不会启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58051100/

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