[英]navigationView setText fatal exception on a null object reference
Ok, so i have been looking around for a solution to this problem and all the solutions I have found are not working. 好的,所以我一直在寻找解决此问题的方法,而我发现的所有解决方案都无法正常工作。 I am trying to change the text of my Sign In Button in my navigationView to Sign Out/Sign In depending on if they are logged in or not and then sending them off to sign in or out if they press the button. 我正在尝试将我的navigationView中的“登录”按钮的文本更改为“注销/登录”,具体取决于它们是否已登录,然后在按下按钮时将其发送以登录或注销。 For some reason my app is crashing with a fatal exception on a null object reference(some of the Log below). 出于某种原因,我的应用程序在空对象引用(以下某些日志)上发生致命异常而崩溃。 What am I doing wrong? 我究竟做错了什么?
I created "signInOutText();" 我创建了“ signInOutText();” so i could try placing it in different spots, it is currently in onResume but i have tried in onCreate as well and originally had it all in onResume. 所以我可以尝试将其放置在不同的位置,它目前在onResume中,但我也尝试过在onCreate中,最初将其全部放在onResume中。 Hopefully I am just overlooking something simple. 希望我只是忽略一些简单的事情。 I cropped out some of my code from MainActivity.java below so it was not so long but let me know if you need to see the full thing. 我从下面的MainActivity.java中裁剪了一些代码,虽然时间不长,但是让我知道您是否需要查看全部内容。
MainActivity.java 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 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"
/>
menu/drawer.xml menu / 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>
Thanks for the help! 谢谢您的帮助!
Ok, so I realized that I was trying to update a menu "item" and not a textView so that made a big difference. 好的,所以我意识到我正在尝试更新菜单“项目”而不是textView,因此起了很大的作用。 Here is the code I used in my signInOutText() to get it to work: 这是我在signInOutText()中使用的代码,以使其正常工作:
private void signInOutText() {
// get menu from navigationView
Menu menu = navigationView.getMenu();
// find MenuItem you want to change
MenuItem nav_signIn = menu.findItem(R.id.signIn);
//Check if user logged in, change sign in/out button to correct text
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
nav_signIn.setTitle(R.string.sign_out);
} else {
nav_signIn.setTitle(R.string.sign_in);
}
}
I hope it helps someone! 希望对您有所帮助! Thanks for the help! 谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.