我只是想使用Google Firebase制作一个电子商务应用程序。我无法解决此问题。该应用程序无法正常运行。当我打开应用程序时,它会关闭并显示一条消息“不幸的是,应用程序名称已停止工作”
这是我的原木猫
05-03 22:57:14.128 2789-2789 / com.eternity.yellaecom E / AndroidRuntime:致命异常:主要 流程:com.eternity.yellaecom,PID:2789 java.lang.NullPointerException:无法为child()中的参数'pathString'传递null 位于com.google.firebase.database.DatabaseReference.child(未知来源) 位于com.google.firebase.database.DataSnapshot.child(未知来源) 在com.eternity.yellaecom.MainActivity $ 3.onDataChange(MainActivity.java:83) com.google.firebase.database.zzp.onDataChange(未知来源) 在com.google.android.gms.internal.zzegf.zza(未知来源) com.google.android.gms.internal.zzeia.zzbyc(未知来源) com.google.android.gms.internal.zzeig.run(未知来源) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5437) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是MainActivity.java代码
package com.eternity.yellaecom;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.eternity.yellaecom.Model.Users;
import com.eternity.yellaecom.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import io.paperdb.Paper;
public class MainActivity extends AppCompatActivity {
private Button joinNowButton, loginButton;
private ProgressDialog loadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
joinNowButton = (Button) findViewById(R.id.main_join_now_btn);
loginButton = (Button) findViewById(R.id.main_login_btn);
loadingBar = new ProgressDialog(this);
Paper.init(this);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
joinNowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
String UserPhoneKey = Paper.book().read(Prevalent.UserPhoneKey);
String UserPasswordKey = Paper.book().read(Prevalent.UserPasswordKey);
if (UserPhoneKey != "" && UserPasswordKey != "")
{
if (!TextUtils.isEmpty(UserPhoneKey) && !TextUtils.isEmpty(UserPasswordKey));
{
AllowAccess(UserPhoneKey, UserPasswordKey);
loadingBar.setTitle("Already Logged in");
loadingBar.setMessage("Please wait....");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
}
}
}
private void AllowAccess(final String phone, final String password)
{
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child("Users").child(phone).exists())
{
Users usersData = dataSnapshot.child("Users").child(phone).getValue(Users.class);
if (usersData.getPhone().equals(phone))
{
if (usersData.getPassword().equals(password))
{
Toast.makeText(MainActivity.this, "Please wait, you are already logged in...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
Prevalent.currentOnlineUser = usersData;
startActivity(intent);
}
else
{
loadingBar.dismiss();
Toast.makeText(MainActivity.this, "Password is incorrect.", Toast.LENGTH_SHORT).show();
}
}
}
else
{
Toast.makeText(MainActivity.this, "Account with this " + phone + " number do not exists.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}