Ok, also habe ich nach einer Lösung für dieses Problem gesucht und alle Lösungen, die ich gefunden habe, funktionieren nicht. Ich versuche, den Text meiner Anmeldungsschaltfläche in meiner navigationView zu ändern, um sich abzumelden, je nachdem, ob sie angemeldet sind oder nicht, und sie dann zum Anmelden oder Abmelden senden, wenn sie die Schaltfläche drücken. Aus irgendeinem Grund stürzt meine App mit einer schwerwiegenden Ausnahme bei einem Verweis auf ein Null-Objekt ab (einige der folgenden Logs). Was mache ich falsch?navigationView setText fatale Ausnahme auf eine Null Objektreferenz
Ich erstellte "signInOutText();" also könnte ich versuchen, es an verschiedenen Stellen zu platzieren, es ist derzeit in onResume, aber ich habe es in onCreate versucht und ursprünglich hatte alles in onResume. Hoffentlich übersehe ich nur etwas Einfaches. Ich habe etwas von meinem Code von MainActivity.java unten abgeschnitten, so dass es nicht so lange war, aber lassen Sie mich wissen, wenn Sie die ganze Sache sehen müssen.
MainActivity.java
public class MainActivity extends AppCompatActivity {
//Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
String category;
MenuItem mPreviousMenuItem;
private ProgressDialog mProgressDialog;
String providerId,uid,name,email;
Uri photoUrl;
private static final String TAG = "MainActivity";
// set name of your preferences list
private static String MY_PREFERENCES = "Settings";
String userNameLoad, emailLoad, profileLoad;
TextView usernameTextView, emailTextView, signInTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setCheckable(true);
menuItem.setChecked(true);
if (mPreviousMenuItem != null) {
mPreviousMenuItem.setChecked(false);
}
mPreviousMenuItem = menuItem;
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with fragments
case R.id.home:
//code for button
break;
case R.id.cat1:
//code for button
break;
case R.id.signIn:
signInOut();
break;
default:
break;
}
return true;
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
private void signInOutText() {
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Get header xml from navigationView
View header = navigationView.getHeaderView(0);
//Check if user logged in, change sign in sign out button to correct text
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
signInTextView = (TextView) header.findViewById(R.id.signIn);
signInTextView.setText(R.string.sign_out);
} else {
signInTextView = (TextView) header.findViewById(R.id.signIn);
signInTextView.setText(R.string.sign_in);
}
}
private void signInOut(){
//TODO add Code .... if logged in logout
Intent intent = new Intent(MainActivity.this, ChooserActivity.class);
startActivity(intent);
finish();
}
public void setActionBarTitle(String title) {
if(getSupportActionBar() != null){
getSupportActionBar().setTitle(title);
}
}
@Override
public void onResume() {
super.onResume();
// Set title
setActionBarTitle(getString(R.string.app_name));
signInOutText();
}
}
Log
Unable to resume activity {com.test.app/com.test.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(int)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4156)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3361)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at com.test.app.MainActivity.signInOutText(MainActivity.java:203)
at com.test.app.MainActivity.onResume(MainActivity.java:307)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1286)
at android.app.Activity.performResume(Activity.java:6987)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4145)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3361)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
Menü/drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/drawer_group"
android:checkable="true">
<item
android:id="@+id/home"
android:checked="false"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/home_string" />
</group>
<group android:id="@+id/group_cat"
android:checkable="true">
<item
android:id="@+id/menuCategories"
android:title="@string/categories_string"
android:layout_gravity="bottom">
<menu>
<item
android:id="@+id/cat1"
android:checked="false"
android:icon="@drawable/ic_inbox_black"
android:title="@string/cat1_string" />
</menu>
</item>
</group>
<group android:id="@+id/group_signInOut"
android:checkable="true">
<item
android:id="@+id/signIn"
android:checked="false"
android:icon="@drawable/ic_power_settings_new_black_24dp"
android:title="@string/sign_in" />
</group>
</menu>
Danke für die Hilfe!