当我点击我的导航抽屉(HomePage.java)中的帐户转移到帐户活动(account.java)。当我尝试点击返回帐户(account.java)我的应用程序停止。让我看看谁的专业编码器会帮助我。把它作为挑战。我尝试了很多,但没有任何帮助我。完全看到我的问题和我的代码。我是Android的新手帮我。我为这个错误而生气。
11-10 00:03:56.739 12243-12243/com.example.hppc.homeservices E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hppc.homeservices, PID: 12243
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hppc.homeservices/com.example.hppc.homeservices.HomePage}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.os.Bundle.getCharSequence(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.os.Bundle.getCharSequence(java.lang.String)' on a null object reference
at com.example.hppc.homeservices.HomePage.onCreate(HomePage.java:33)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
NavigationDrawer Activity.java(HomePage.java)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle b = getIntent().getExtras();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView txtProfileName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.tvEmailHome);
txtProfileName.setText(b.getCharSequence("Email"));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Account) {
startActivity(new Intent(HomePage.this,Account.class));
finish();
} if (id == R.id.nav_Services) {
} if (id == R.id.nav_settings) {
} if (id == R.id.nav_Help) {
} if (id == R.id.LogOut) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(HomePage.this,MainActivity.class));
finish();
} if (id == R.id.nav_share) {
} if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
Account.java(导航栏中的我的帐户)
private DatabaseReference databaseReference;
private ListView details;
private ArrayList<String> account = new ArrayList<>();
FirebaseUser user;
String uid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
details=(ListView)findViewById(R.id.listview);
user = FirebaseAuth.getInstance().getCurrentUser();
uid = user.getUid();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, account);
details.setAdapter(arrayAdapter);
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String user_name = dataSnapshot.child("Name").getValue(String.class);
String user_email = dataSnapshot.child("Email").getValue(String.class);
String user_phone = dataSnapshot.child("Phone").getValue(String.class);
if (user.getEmail().equalsIgnoreCase(user_email)) {
account.add("Name :" + user_name);
account.add("Email ID :" + user_email);
account.add("Phone Number :" + user_phone);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==android.R.id.home){
Intent mainInten = new Intent(this, HomePage.class);
this.startActivity(mainInten);
//this.finish();
//onBackPressed();
}
return super.onOptionsItemSelected(item);
}
public void checking(View view) {
startActivity(new Intent(Account.this,MyAccount.class));
finish();
}
当我点击导航抽屉中的帐户时,我正在转移到“帐户”活动,但当我尝试在该帐户活动顶部的“帐户”活动中点击goback图标时,我的应用程序就会停止。
答案 0 :(得分:0)
我遇到了同样的问题。当我删除动作栏时,它完美无缺。从Account.java中删除以下行。
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
作为建议,不要使用导航抽屉活动。使用片段比活动容易得多。您可以使用FragmentTransaction.addToBackStack(null);
方法轻松返回到先前的片段。
我参考了这个视频教程。它可能会有所帮助。
https://www.youtube.com/watch?v=-SUvA1fXaKw