我想在我的Android Studio片段上显示用户信息。当我完成代码时,没有任何错误。但是,当我在设备上运行应用程序时,当我转到“帐户片段”时,该应用程序将停止工作。当我检查日志猫时说:
java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String com.example.ghifa.mrad.Model.User.getImageurl()'
我不知道该怎么解决。有人能帮我吗?这是学校的任务。
这是我的logcat错误
11-20 17:46:19.829 14051-14051/com.example.ghifa.mrad E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ghifa.mrad, PID: 14051
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.ghifa.mrad.Model.User.getImageurl()' on a null object reference
at com.example.ghifa.mrad.AkunFrg$2.onDataChange(AkunFrg.java:107)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
这是我的Java
ImageView photo;
ImageView back;
TextView topNama;
TextView topEmail;
TextView mSaldo;
TextView mUsername;
TextView mNama;
TextView mEmail;
TextView mHP;
TextView mAlamat;
Button toEdit;
Uri uriProfileimage;
FirebaseUser firebaseUser;
String profileid;
public AkunFrg() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_akun_frg, container, false);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
SharedPreferences prefs = getContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
profileid = prefs.getString("profileid", "none");
photo = view.findViewById(R.id.foto);
topNama = view.findViewById(R.id.ac_top_nama);
topEmail = view.findViewById(R.id.ac_top_email);
mSaldo = view.findViewById(R.id.saldo_akun);
mUsername = view.findViewById(R.id.ac_username);
mNama = view.findViewById(R.id.ac_nama);
mEmail = view.findViewById(R.id.ac_email);
mHP = view.findViewById(R.id.ac_hp);
mAlamat = view.findViewById(R.id.ac_alamat);
toEdit = view.findViewById(R.id.to_edit);
userinfo();
if(profileid.equals(firebaseUser.getUid()))
{
toEdit.setText("Edit Profil");
}
toEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
private void userinfo()
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(profileid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(firebaseUser != null)
{
User user = dataSnapshot.getValue(User.class);
Glide.with(getContext()).load(user.getImageurl()).into(photo);
topNama.setText(user.getNama());
mUsername.setText(user.getUsername());
mSaldo.setText(user.getSaldo());
mNama.setText(user.getNama());
mHP.setText(user.getNo_hp());
mAlamat.setText(user.getAlamat());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:1)
在写这篇文章之前
Glide.with(getContext()).load(user.getImageurl()).into(photo);
确保您检查值存在
像这样
if(user.getImageurl() != null){
Glide.with(getContext()).load(user.getImageurl()).into(photo);
}
或将代码放入try / catch语句中
try{
Glide.with(getContext()).load(user.getImageurl()).into(photo);
}catch(NullPointerException e){
Log.e(TAG, e.toString())
}
希望有帮助
答案 1 :(得分:0)
来自https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html
传入的类的实例,其中填充了来自此实例的数据 快照,如果此位置没有数据,则为空。
我怀疑dataSnapshot.getValue(User.class);
返回null。