Android StartActivity无法启动新活动

时间:2019-09-22 16:17:04

标签: java android start-activity

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

RegisterButtonClicked成功调用了一个活动,但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) 

2 个答案:

答案 0 :(得分:0)

您可以按以下方式更改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);
    }

}

答案 1 :(得分:-1)

如果您要设置来自XML的点击,则无需在代码中设置点击监听器。

 public void RegisterButtonClicked(View view) {
                Log.d("registerButtonClicked", "inside registerButtonClicked");
                        startActivity(new Intent(MainActivity.this, Register.class));
            }

public void LoginButtonClicked(View view) {
                Log.d("LoginButtonClicked", "inside LoginButtonClicked");
                startActivity(new Intent(MainActivity.this, Login2.class));
            }