clicking on the menu item时,我无法转到我的帐户设置页面。当我点击该项目时,error messages会显示在我的logcat上。我尝试了不同的解决方案来解决这个问题,但遗憾的是没有运气。
这是我的database structure。
这是我的 SettingsActivity.java 文件
public class SettingsActivity extends AppCompatActivity {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
String current_uid = mCurrentUser.getUid();
//String current_uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(SettingsActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
这是我的 MainActivity.java
public class MainActivity extends AppCompatActivity {
// Creating an instance of the FirebaseAuth object
private FirebaseAuth mAuth;
private Toolbar mToolbar;
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the object
mAuth = FirebaseAuth.getInstance();
// Set the toolbar
mToolbar = (Toolbar) findViewById(R.id.main_Toolbar);
setSupportActionBar(mToolbar); // allows the toolbar to be displayed on the screen
getSupportActionBar().setTitle("Lapit Chat");
// Tabs
mViewPager = (ViewPager) findViewById(R.id.main_tabPager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.mainTabs);
mTabLayout.setupWithViewPager(mViewPager);
}
@Override
public void onStart() {
super.onStart();
// get the current user logged in via Firebase and store it in the create variable
FirebaseUser currentUser = mAuth.getCurrentUser();
// if there is no user
if (currentUser == null) {
// User is not signed in; show the start activity
sendToStart();
} // if user is logged in, go straight to the main page
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// If the user clicks the logout button
if(item.getItemId() == R.id.menu_Logout) {
// Log the user out of the session
FirebaseAuth.getInstance().signOut();
sendToStart();
}
if(item.getItemId() == R.id.menu_Settings) {
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
}
return true;
}
// Method to send the user back to the start page when logged out
private void sendToStart() {
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startActivity(startIntent);
finish(); // Prevents user from coming back to this page when clicking the back button
}
}
在此活动中,当用户点击"帐户设置"菜单项,它不让我,并显示一个对话框说"不幸的是已停止"。在错误截图中它显示了原因,但即使我已经研究过解决方案,我仍然不理解它。有谁可以向我解释为什么会这样?
感谢您提供任何帮助。
答案 0 :(得分:0)
地点:String current_uid = mCurrentUser.getUid();
以下:mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
如果您在检索当前用户之前getUid()
,则mCurrentUser
变量将为空并导致NullPointerException