谁能帮我这个?我的android应用不断崩溃

时间:2019-09-17 13:18:25

标签: android android-studio

这是我的日志猫所说的: 2019-09-17 21:02:48.887 19276-19276 / com.oopproject.androidbarberbooking E / AndroidRuntime:FATAL EXCEPTION:main         流程:com.oopproject.androidbarberbooking,PID:19276         java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.oopproject.androidbarberbooking.Model.User.getName()'             在com.oopproject.androidbarberbooking.Fragments.HomeFragment.setUserInformation(HomeFragment.java:152)             在com.oopproject.androidbarberbooking.Fragments.HomeFragment.onCreateView(HomeFragment.java:94)

这是我的家庭活动:

private void setUserInformation() public class HomeActivity extends AppCompatActivity {

@BindView(R.id.bottom_navigation)
BottomNavigationView bottomNavigationView;

BottomSheetDialog bottomSheetDialog;

CollectionReference userRef;

AlertDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ButterKnife.bind(HomeActivity.this);

    userRef = FirebaseFirestore.getInstance().collection("User");
    dialog = new SpotsDialog.Builder().setContext(this).setCancelable(false).build();

    //Checked if Logged in
    if (getIntent() != null)
    {
        boolean isLogin = getIntent().getBooleanExtra(Common.IS_LOGIN,false);
        if (isLogin)
        {
            dialog.show();

            //Check if user  exist
            AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(Account account) {
                    if (account != null)
                    {
                        final DocumentReference currentUser = userRef.document(account.getPhoneNumber().toString());
                        currentUser.get()
                                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                        if (task.isSuccessful())
                                        {
                                            DocumentSnapshot userSnapShot = task.getResult();
                                            if (!userSnapShot.exists())
                                            {
                                                showUpdateDialog(account.getPhoneNumber().toString());
                                            }
                                            else
                                            {
                                                //if there is already a registered user
                                                Common.currentUser = userSnapShot.toObject(User.class);
                                                bottomNavigationView.setSelectedItemId(R.id.action_home);
                                            }
                                            if (dialog.isShowing())
                                                dialog.dismiss();
                                        }
                                    }
                                });
                    }
                }

                @Override
                public void onError(AccountKitError accountKitError) {
                    Toast.makeText(HomeActivity.this, ""+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        Fragment fragment = null;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.action_home) {
                fragment = new HomeFragment();
            } else if (menuItem.getItemId() == R.id.action_shop)
                fragment = new ShopFragment();


            return loadFragment(fragment);
        }
    });

}

private boolean loadFragment(Fragment fragment) {
    if (fragment != null)
    {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment)
                .commit();
        return true;
    }
    return false;
}

private void showUpdateDialog(final String phoneNumber) {

    bottomSheetDialog = new BottomSheetDialog(this);
    bottomSheetDialog.setTitle("One More Step!");
    bottomSheetDialog.setCanceledOnTouchOutside(false);
    bottomSheetDialog.setCancelable(false);
    View sheetView = getLayoutInflater().inflate(R.layout.layout_update_information,null);

    Button btn_update = (Button)sheetView.findViewById(R.id.btn_update);
    final TextInputEditText edt_name = (TextInputEditText)sheetView.findViewById(R.id.edt_name);
    final TextInputEditText edt_address = (TextInputEditText)sheetView.findViewById(R.id.edt_address);

    btn_update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!dialog.isShowing())
                dialog.show();

            User user = new User(edt_name.getText().toString(),
                    edt_address.getText().toString(),
                    phoneNumber);
            userRef.document(phoneNumber)
                    .set(user)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            bottomSheetDialog.dismiss();
                            if (dialog.isShowing())
                                dialog.dismiss();

                            Common.currentUser = user;
                            bottomNavigationView.setSelectedItemId(R.id.action_home);

                            Toast.makeText(HomeActivity.this, "Thank You!", Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    bottomSheetDialog.dismiss();
                    if (dialog.isShowing())
                        dialog.dismiss();
                    Toast.makeText(HomeActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

    bottomSheetDialog.setContentView(sheetView);
    bottomSheetDialog.show();
}

}

和我的家庭分段行150-154,其中再次使用Common.current用户:

private void setUserInformation() {
    layout_user_information.setVisibility(View.VISIBLE);
    txt_user_name.setText(Common.currentUser.getName());

}

这是我的“模型用户” java类中的内容:

package com.oopproject.androidbarberbooking.Model;

public class User {
      private String name,address,phoneNumber;

      public User() {
      }

      public User(String name, String address, String phoneNumber) {
            this.name = name;
            this.address = address;
            this.phoneNumber = phoneNumber;
      }

      public String getName() {
      return name;
      }

      public void setName(String name) {
           this.name = name;
      }

      public String getAddress() {
           return address;
      }

      public void setAddress(String address) {
           this.address = address;
      }

      public String getPhoneNumber() {
           return phoneNumber;
      }

      public void setPhoneNumber(String phoneNumber) {
           this.phoneNumber = phoneNumber;
      }
}

1 个答案:

答案 0 :(得分:0)

从堆栈跟踪中您可以看到您的用户丢失。并在使用NullPointerException期间抛出om.oopproject.androidbarberbooking.Model.User

检查为什么您对Common.currentUser的实现返回空值。这是一个问题。

相关问题