これで、この問題の解決策を探していました。見つかったすべての解決策は機能していません。ログインしているかどうかに応じてSign In/Sign InをナビゲーションビューのSign In Buttonのテキストに変更しようとしています。ボタンを押すとサインインまたはサインアウトするために送信します。何らかの理由で、私のアプリケーションがヌルオブジェクトリファレンス(以下のログの一部)で致命的な例外を発生してクラッシュしています。私は間違って何をしていますか?navigationView setText nullオブジェクト参照で致命的な例外が発生する
私は "signInOutText();"を作成しました。だから私は別の場所に配置しようとすることができます、それは現在onResumeにありますが、私もonCreateで試して、元々onResumeでそれをすべて持っていた。うまくいけば、私は単純なものを見落としているだけです。私は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();
}
}
ログ
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
助けのためのメニュー/ 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>
ありがとう!