英文:
I am having a java null exception
问题
这是我的资料片段,在这里我遇到了一个Java空指针异常。我尝试在我的资料片段中添加在线状态功能,但是出现了这个错误。请帮忙,我需要在今天完成这部分。我的资料片段在下面,还有日志记录:
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private FirebaseMethods mFirebaseMethods;
private FirebaseUser firebaseUser;
//widgets
private TextView mPosts, mFollowers, mFollowing, mDisplayName, mUsername, mWebsite, mDescription;
private ProgressBar mProgressBar;
private CircleImageView mProfilePhoto;
private GridView gridView;
private Toolbar toolbar;
private ImageView profileMenu;
private BottomNavigationViewEx bottomNavigationView;
private Context mContext;
//vars
private int mFollowersCount = 0;
private int mFollowingCount = 0;
private int mPostsCount = 0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mDisplayName = (TextView) view.findViewById(R.id.display_name);
mUsername = (TextView) view.findViewById(R.id.username);
mWebsite = (TextView) view.findViewById(R.id.website);
mDescription = (TextView) view.findViewById(R.id.description);
mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
mPosts = (TextView) view.findViewById(R.id.tvPosts);
mFollowers = (TextView) view.findViewById(R.id.tvFollowers);
mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mProgressBar = (ProgressBar) view.findViewById(R.id.profileProgressBar);
gridView = (GridView) view.findViewById(R.id.gridView);
toolbar = (Toolbar) view.findViewById(R.id.profileToolBar);
profileMenu = (ImageView) view.findViewById(R.id.profileMenu);
bottomNavigationView = (BottomNavigationViewEx) view.findViewById(R.id.bottomNavViewBar);
mContext = getActivity();
mFirebaseMethods = new FirebaseMethods(getActivity());
Log.d(TAG, "onCreateView: started.");
setupBottomNavigationView();
setupToolbar();
setupFirebaseAuth();
setupGridView();
getFollowersCount();
getFollowingCount();
getPostsCount();
TextView editProfile = (TextView) view.findViewById(R.id.textEditProfile);
editProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
intent.putExtra(getString(R.string.calling_activity), getString(R.string.profile_activity));
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
return view;
}
private void status(String status){
myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("status", status);
myRef.updateChildren(hashMap);
}
@Override
public void onResume() {
super.onResume();
status("online");
}
@Override
public void onPause() {
super.onPause();
status("offline");
}
以下是日志记录:
2020-10-15 13:30:36.012 16287-16287/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 16287
java.lang.RuntimeException: Unable to resume activity {umo.com.players/umo.com.players.Profile.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3718)
...
英文:
here is my profile fragment where am having a java null
I tried to add an online status feature in my profile fragment and I got this error
Please help
I need to finish this part today
My profile fragment is down below also the log cat
here is my profile fragment where am having a java null
I tried to add an online status feature in my profile fragment and I got this error
Please help
I need to finish this part today
My profile fragment is down below also the log cat
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private FirebaseMethods mFirebaseMethods;
private FirebaseUser firebaseUser;
//widgets
private TextView mPosts, mFollowers, mFollowing, mDisplayName, mUsername, mWebsite, mDescription;
private ProgressBar mProgressBar;
private CircleImageView mProfilePhoto;
private GridView gridView;
private Toolbar toolbar;
private ImageView profileMenu;
private BottomNavigationViewEx bottomNavigationView;
private Context mContext;
//vars
private int mFollowersCount = 0;
private int mFollowingCount = 0;
private int mPostsCount = 0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mDisplayName = (TextView) view.findViewById(R.id.display_name);
mUsername = (TextView) view.findViewById(R.id.username);
mWebsite = (TextView) view.findViewById(R.id.website);
mDescription = (TextView) view.findViewById(R.id.description);
mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
mPosts = (TextView) view.findViewById(R.id.tvPosts);
mFollowers = (TextView) view.findViewById(R.id.tvFollowers);
mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mProgressBar = (ProgressBar) view.findViewById(R.id.profileProgressBar);
gridView = (GridView) view.findViewById(R.id.gridView);
toolbar = (Toolbar) view.findViewById(R.id.profileToolBar);
profileMenu = (ImageView) view.findViewById(R.id.profileMenu);
bottomNavigationView = (BottomNavigationViewEx) view.findViewById(R.id.bottomNavViewBar);
mContext = getActivity();
mFirebaseMethods = new FirebaseMethods(getActivity());
Log.d(TAG, "onCreateView: stared.");
setupBottomNavigationView();
setupToolbar();
setupFirebaseAuth();
setupGridView();
getFollowersCount();
getFollowingCount();
getPostsCount();
TextView editProfile = (TextView) view.findViewById(R.id.textEditProfile);
editProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
intent.putExtra(getString(R.string.calling_activity), getString(R.string.profile_activity));
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
return view;
}
private void status(String status){
myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("status", status);
myRef.updateChildren(hashMap);
}
@Override
public void onResume() {
super.onResume();
status("online");
}
@Override
public void onPause() {
super.onPause();
status("offline");
}
Here is the log cat
2020-10-15 13:30:36.012 16287-16287/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 16287
java.lang.RuntimeException: Unable to resume activity {umo.com.players/umo.com.players.Profile.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3718)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2966)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6739)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at umo.com.players.Profile.ProfileFragment.status(ProfileFragment.java:140)
at umo.com.players.Profile.ProfileFragment.onResume(ProfileFragment.java:151)
at android.support.v4.app.Fragment.performResume(Fragment.java:2308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1465)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3227)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3191)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:482)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:471)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
at android.app.Activity.performResume(Activity.java:7177)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3693)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2966) 
at android.app.ActivityThread.-wrap12(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6739) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
答案1
得分: -1
你的错误信息是FirebaseUser.getUid() on a null object reference
,出错的位置在这一行:
myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
根据我在你的代码中看到的,你并没有初始化firebaseUser
变量,所以在调用其实例的getUid()
方法之前,你需要先初始化firebaseUser
,然后你的问题应该就会解决。
我从这个链接中找到了这个参考代码:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// 用户已登录
} else {
// 没有用户登录
}
英文:
Your error says FirebaseUser.getUid() on a null object reference
and its on the line
myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
and as I see in your code you have not initialized firebaseUser
variable, so you need to initialize firebaseUser
before calling getUid()
method on its instance, and you should be fine after that.
I got this referencece from this link
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
} else {
// No user is signed in
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论