如何解决此问题:尝试调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)

问题描述 投票:0回答:1

我是在 Android studio 上创建移动应用程序的新手。 我收到一个错误,如下所示

Logcat展示

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)'

在 comm.example.login_logout.Login.onCreate(Login.java:64)

// 我转到这条线,但我不知道。

当我运行应用程序时,它可以构建,但仍然崩溃。

我的xml文件:activity_login.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".Login" android:orientation="vertical" android:gravity="center" android:padding="15dp"> <!--Login--> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:gravity="center" android:text="@string/login" android:textSize="25sp" android:textStyle="bold" /> <!--Email--> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <com.google.android.material.textfield.TextInputEditText android:id="@+id/email" android:hint="@string/email" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout> <!--Password--> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <com.google.android.material.textfield.TextInputEditText android:id="@+id/password" android:hint="@string/password" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout> <ProgressBar android:id="@+id/progressBar" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--Button--> <Button android:id="@+id/btn_login" android:text="@string/login" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textStyle="bold" android:textSize="15sp" android:gravity="center" android:layout_marginTop="20dp" android:id="@+id/registerNow" android:text="@string/click_to_register" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>

Java 文件: Login.java

package com.example.login_logout; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.login_logout.R.id; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.material.textfield.TextInputEditText; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import java.util.concurrent.CountedCompleter; public class Login extends AppCompatActivity { TextInputEditText editTextEmail, editTextPassword; Button buttonLogin; FirebaseAuth mAuth; ProgressBar progressBar; TextView textView; @Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); if(currentUser != null){ Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } } @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); mAuth = FirebaseAuth.getInstance(); editTextEmail = findViewById(R.id.email); editTextPassword = findViewById(R.id.password); buttonLogin = findViewById(R.id.btn_login); progressBar = findViewById(R.id.progressBar); textView = findViewById(R.id.registerNow); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getApplicationContext(), Register.class); startActivity(intent); //finish(); } }); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressBar.setVisibility(View.VISIBLE); String email,password; email = String.valueOf(editTextEmail.getText()); password = String.valueOf(editTextPassword.getText()); // check it empty or not if (TextUtils.isEmpty(email)) { Toast.makeText(Login.this,"Enter email", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(Login.this,"Enter password", Toast.LENGTH_SHORT).show(); return; } mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { progressBar.setVisibility(View.GONE); if (task.isSuccessful()) { Toast.makeText(getApplicationContext(), "Login Successful",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(Login.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } } }); } }); } }

如何解决这个问题?

java android firebase
1个回答
0
投票
很明显您在登录活动中使用“R.layout.activity_register”。正如您在问题中提到的那样,它应该是“R.layout.activity_login”

© www.soinside.com 2019 - 2024. All rights reserved.