我的Java代码在这里 奇怪的行为。我不知道发生了什么。请解决这个问题。 我认为问题绑定导航heeader。我想从firebase中检索数据并设置在导航头中。请帮助我。提前谢谢。 发生了什么,我不知道。
奇怪的行为。我不知道发生了什么。请解决这个问题。 我认为问题绑定导航heeader。我想从firebase中检索数据并设置在导航头中。请帮助我。提前谢谢。 发生了什么,我不知道。
public class HomeActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference mReference;
private FirebaseUser currentUser;
private boolean twice;
private ViewPager mViewPager;
private TabLayout mTabLayout;
private SectionPageAdapter mSectionPageAdapter;
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private ActionBarDrawerToggle mToggle;
private String UID;
private TextView navUserName , navBloodGroup , mNavHeaderEmail;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
/* mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
*/
// Firebase contents
currentUser = FirebaseAuth.getInstance().getCurrentUser();
mReference = FirebaseDatabase.getInstance().getReference();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// This action use for access drawer component
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = mNavigationView.getHeaderView(0);
navUserName = (TextView) headerView.findViewById(R.id.navDispProfileName);
navBloodGroup = (TextView) headerView.findViewById(R.id.nav_dis_Blood);
mNavHeaderEmail = (TextView) headerView.findViewById(R.id.navHeaderEmail);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAuth = FirebaseAuth.getInstance();
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mTabLayout = (TabLayout) findViewById(R.id.main_tab);
// Set Adapter in View pager
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionPageAdapter);
// For Tab Layout
mTabLayout.setupWithViewPager(mViewPager);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.account:
startActivity(new Intent(getApplication(), EditProfileActivity.class));
break;
case R.id.logout:
if (currentUser != null) {
mAuth.signOut();
startActivity(new Intent(getApplication(), FirstActivity.class));
}
break;
}
mNavigationView.setCheckedItem(id);
mDrawerLayout.closeDrawers();
return true;
}
});
...... In here i got error. when i open again my application after sign out from firebase. mReference.child("Users").child(currentUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Retrieve Value from database
String firstName =(String) dataSnapshot.child("FirstName").getValue();
String lastName = (String)dataSnapshot.child("LastName").getValue();
String bloodGroup = (String)dataSnapshot.child("BloodGroup").getValue();
String email = (String)dataSnapshot.child("Email").getValue();
// Set value in TextView
String completeName = firstName + " " + lastName;
navUserName.setText(completeName);
navBloodGroup.setText(bloodGroup);
mNavHeaderEmail.setText(email);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@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 (mToggle.onOptionsItemSelected(item)) {
return true;
}
/* if (item.getItemId() == R.id.btn_logout) {
if(currentUser != null) {
startActivity(new Intent(this, FirstActivity.class));
mAuth.signOut();
}
}
else if(item.getItemId() == R.id.account_seting){
// replaceFragment(new UserProfile());
startActivity(new Intent(this,MenuActivity.class));
}*/
return true;
}
// method for back press close application
@Override
public void onBackPressed() {
if (twice == true) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
twice = true;
Toast.makeText(this, "Please Again to Close Application", Toast.LENGTH_SHORT).show();
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
twice = false;
}
}, 3000);
}
@Override
protected void onStart() {
super.onStart();
if(currentUser == null){
startActivity(new Intent(this , FirstActivity.class));
}
}
}
Process: com.example.adil.bloodbankapplication, PID: 3071
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adil.bloodbankapplication/com.example.adil.bloodbankapplication.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.adil.bloodbankapplication.HomeActivity.onCreate(HomeActivity.java:105)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:-1)
嗯,你在第105行有一个NPE的错误。有些东西是无效的,不应该是。
at com.example.adil.bloodbankapplication.HomeActivity.onCreate(HomeActivity.java:105)
让我们知道那条线是什么,也许我们可以提供更多帮助。