gpt4 book ai didi

java - ProgressBar 空点异常导致应用程序崩溃

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

我是 Android 应用程序开发新手,正在处理一个项目。我收到以下错误,但我似乎无法找出解决方案,它使我的应用程序崩溃。

我有一个连接到 firebase 并注册用户并将其详细信息保存到数据库的应用程序。但是,当我打开注册用户 Activity 时,应用程序崩溃了,因此无法进一步进行。请帮忙,因为我对 Android 应用程序开发还很陌生。

logcat 在这里

2020-05-27 01:08:00.427 25788-25788/com.example.auth E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.auth, PID: 25788
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.auth/com.example.auth.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.example.auth.RegisterActivity.onCreate(RegisterActivity.java:35)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2020-05-27 01:08:00.452 25788-25788/com.example.auth I/Process: Sending signal. PID: 25788 SIG: 9

这是RegisterActivity.java

package com.example.auth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

private EditText editTextName, editTextEmail, editTextPassword, editTextPhone;
private ProgressBar progressBar;

private FirebaseAuth mAuth;

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

editTextName = findViewById(R.id.edit_text_name);
editTextEmail = findViewById(R.id.edit_text_email);
editTextPassword = findViewById(R.id.edit_text_password);
editTextPhone = findViewById(R.id.edit_text_phone);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
**progressBar.setVisibility(View.GONE);**

mAuth = FirebaseAuth.getInstance();

findViewById(R.id.button_register).setOnClickListener(this);
}

@Override
protected void onStart() {
super.onStart();

if (mAuth.getCurrentUser() != null) {
//handle the already login user
}
}

private void registerUser() {
final String name = editTextName.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
final String phone = editTextPhone.getText().toString().trim();

if (name.isEmpty()) {
editTextName.setError(getString(R.string.input_error_name));
editTextName.requestFocus();
return;
}

if (email.isEmpty()) {
editTextEmail.setError(getString(R.string.input_error_email));
editTextEmail.requestFocus();
return;
}

if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editTextEmail.setError(getString(R.string.input_error_email_invalid));
editTextEmail.requestFocus();
return;
}

if (password.isEmpty()) {
editTextPassword.setError(getString(R.string.input_error_password));
editTextPassword.requestFocus();
return;
}

if (password.length() < 6) {
editTextPassword.setError(getString(R.string.input_error_password_length));
editTextPassword.requestFocus();
return;
}

if (phone.isEmpty()) {
editTextPhone.setError(getString(R.string.input_error_phone));
editTextPhone.requestFocus();
return;
}

if (phone.length() != 10) {
editTextPhone.setError(getString(R.string.input_error_phone_invalid));
editTextPhone.requestFocus();
return;
}


progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if (task.isSuccessful()) {

User user = new User(
name,
email,
phone
);

FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, getString(R.string.registration_success), Toast.LENGTH_LONG).show();
} else {
//display a failure message
}
}
});

} else {
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_register:
registerUser();
break;
}
}
}

这是activity_register.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="com.example.auth.RegisterActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="@dimen/default_padding">

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="@color/colorText"
android:textSize="@dimen/app_name_size" />

<EditText
android:id="@+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_name"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/full_name"
android:nextFocusDown="@id/edit_text_email"
android:textColor="@color/colorText"
android:textColorHint="@color/colorHint" />


<EditText
android:id="@+id/edit_text_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_email"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:nextFocusDown="@id/edit_text_password"
android:textColor="@color/colorText"
android:textColorHint="@color/colorHint" />


<EditText
android:id="@+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_password"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/password"
android:inputType="textPassword"
android:nextFocusDown="@id/edit_text_phone"
android:textColor="@color/colorText"
android:textColorHint="@color/colorHint" />

<EditText
android:id="@+id/edit_text_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:drawableLeft="@drawable/ic_phone"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/phone"
android:inputType="number"
android:maxLength="10"
android:nextFocusDown="@id/button_register"
android:textColor="@color/colorText"
android:textColorHint="@color/colorHint" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@id/radioButton1"

android:orientation="horizontal">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:text="Student" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:text="Institute" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="267dp"
android:layout_height="wrap_content"
android:text="Teacher" />
</RadioGroup>

<Button
android:id="@+id/button_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_button_register"
android:text="@string/register"
android:textAllCaps="false"
android:textColor="@color/colorText" />

</LinearLayout>

<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />

</RelativeLayout>

最佳答案

这是一个错字:

setContentView(R.layout.activity_main);

但您的布局实际上是 activity_register,而不是 activity_main

您的意思可能是:

setContentView(R.layout.activity_register);

关于java - ProgressBar 空点异常导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62030173/

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