有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java致命异常:main(Android标准)

当我在我的设备上运行时,它将崩溃,我的日志如下:

2020-12-19 17:03:28.204 24798-24798/com.example.emergencynotificationhealthcare E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: com.example.emergencynotificationhealthcare, PID: 24798
java.lang.RuntimeException: Unable to start activity       
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2799)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
    at 安卓.app.ActivityThread.-wrap12(ActivityThread.java)
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1573)
    at 安卓.os.Handler.dispatchMessage(Handler.java:105)
    at 安卓.os.Looper.loop(Looper.java:156)
    at 安卓.app.ActivityThread.main(ActivityThread.java:6605)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:889)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 
 安卓.view.View.setOnClickListener(安卓.view.View$OnClickListener)' on a null object reference
    at com.example.emergencynotificationhealthcare.Login.onCreate(Login.java:42)
    at 安卓.app.Activity.performCreate(Activity.java:6964)
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)

这是我的活动登记簿。xml:

<ImageView
    安卓:layout_width="101dp"
    安卓:layout_height="69dp"
    安卓:transitionName="logo_logo"
    安卓:src="@drawable/logo_new"/>

<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="-10dp"
    安卓:fontFamily="@font/bungee"
    安卓:text="Welcome"
    安卓:transitionName="logo_text"
    安卓:textColor="#ff8e88"
    安卓:textSize="30sp" />

<TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Register to start"
    安卓:textSize="16sp"/>

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="vertical"
    安卓:layout_marginTop="10dp">

    <com.google.安卓.material.textfield.TextInputLayout
        安卓:id="@+id/username"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="Username"
        app:counterEnabled="true"
        app:counterMaxLength="15"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.安卓.material.textfield.TextInputEditText
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:inputType="text"
            安卓:transitionName="user_tran"/>

    </com.google.安卓.material.textfield.TextInputLayout>

    <com.google.安卓.material.textfield.TextInputLayout
    安卓:id="@+id/email"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:hint="Email"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.安卓.material.textfield.TextInputEditText
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:inputType="textEmailAddress"/>

    </com.google.安卓.material.textfield.TextInputLayout>

    <com.google.安卓.material.textfield.TextInputLayout
        安卓:id="@+id/phoneNo"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="Phone Number"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.安卓.material.textfield.TextInputEditText
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:inputType="number"/>

    </com.google.安卓.material.textfield.TextInputLayout>

    <com.google.安卓.material.textfield.TextInputLayout
        安卓:id="@+id/password"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="Password"
        app:passwordToggleEnabled="true"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <com.google.安卓.material.textfield.TextInputEditText
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:inputType="textPassword"
            安卓:transitionName="password_tran"/>

    </com.google.安卓.material.textfield.TextInputLayout>

</LinearLayout>

<TextView
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:text="User Type"
    安卓:layout_marginLeft="5dp"
    安卓:textSize="17dp"/>

<CheckBox
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:text="User" />

<CheckBox
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:text="Guardian"/>

<Button
    安卓:id="@+id/login_btn"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="50dp"
    安卓:background="#ff8e88"
    安卓:text="Register"
    安卓:textColor="#fff"
    安卓:transitionName="button_tran"/>

<Button
    安卓:id="@+id/register_screen"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:text="Already have an account? LOGIN"
    安卓:background="#00000000"
    安卓:transitionName="login_register_tran"/>

这是我的登录名。爪哇:

Button callRegister, login_btn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_login);

    callRegister = findViewById(R.id.register_screen);
    image = findViewById(R.id.imageView);
    logoText = findViewById(R.id.logo_name);
    sloganText = findViewById(R.id.slogan_name);
    username = findViewById(R.id.username);
    password = findViewById(R.id.password);
    login_btn = findViewById(R.id.login_btn);

    callRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Login.this,Register.class);

            Pair[] pairs = new Pair[7];

            pairs[0] = new Pair<View,String>(image,"logo_logo");
            pairs[1] = new Pair<View,String>(logoText,"logo_text");
            pairs[2] = new Pair<View,String>(sloganText,"logo_desc");
            pairs[3] = new Pair<View,String>(username,"user_tran");
            pairs[4] = new Pair<View,String>(password,"password_tran");
            pairs[5] = new Pair<View,String>(login_btn,"button_tran");
            pairs[6] = new Pair<View,String>(callRegister,"login_register_tran");

            if (安卓.os.Build.VERSION.SDK_INT >= 安卓.os.Build.VERSION_CODES.LOLLIPOP) {
                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
                startActivity(intent, options.toBundle());
            }
        }
    });

}

这是我的活动登录。xml

<ImageView
    安卓:layout_width="101dp"
    安卓:layout_height="69dp"
    安卓:src="@drawable/logo_new"
    安卓:transitionName="logo_logo"/>

<TextView
    安卓:id="@+id/logo_name"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:fontFamily="@font/bungee"
    安卓:text="Hi, Welcome Back!"
    安卓:transitionName="logo_text"
    安卓:textColor="#ff8e88"
    安卓:textSize="40sp"/>

<TextView
    安卓:id="@+id/slogan_name"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Sign in to continue"
    安卓:transitionName="logo_desc"
    安卓:textSize="18sp"/>

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_margin="5dp"
    安卓:layout_marginTop="10dp"
    安卓:layout_marginBottom="20dp"
    安卓:orientation="vertical">

    <com.google.安卓.material.textfield.TextInputLayout
        安卓:id="@+id/username"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="Username">

        <com.google.安卓.material.textfield.TextInputEditText
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"/>
    </com.google.安卓.material.textfield.TextInputLayout>

    <com.google.安卓.material.textfield.TextInputLayout
        安卓:id="@+id/password"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:hint="Password"
        app:passwordToggleEnabled="true">

        <com.google.安卓.material.textfield.TextInputEditText
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:inputType="textPassword" />

    </com.google.安卓.material.textfield.TextInputLayout>

    <CheckBox
        style="@style/Widget.AppCompat.CompoundButton.CheckBox"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="Remember Me" />

    <Button
        安卓:layout_width="160dp"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="right"
        安卓:layout_margin="3dp"
        安卓:background="#00000000"
        安卓:elevation="0dp"
        安卓:text="Forget Password" />

    <Button
        安卓:id="@+id/btnLogin"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginTop="5dp"
        安卓:layout_marginBottom="5dp"
        安卓:background="#ff8e88"
        安卓:text="Log In"
        安卓:textColor="#fff" />

    <Button
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="right"
        安卓:layout_margin="5dp"
        安卓:background="#00000000"
        安卓:elevation="0dp"
        安卓:fontFamily="@font/antic"
        安卓:text="New Member? REGISTER"
        安卓:textColor="#000" />

</LinearLayout>

</LinearLayout>

共 (1) 个答案

  1. # 1 楼答案

    您是否对正确的布局文件进行了充气activity_register是您所说的文件名,但您的代码使用 setContentView(R.layout.activity_login);

    如果不是这样的话,试着用setContentView(R.layout.activity_register);替换它