Answer the question
In order to leave comments, you need to log in
How to solve On a null object reference?
Good afternoon! Help solve the problem.
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.gnegno.horoscope.FragmentFirst.sendData(java.lang.String)' on a null object reference
at com.gnegno.horoscope.startActivity.onNavigationItemSelected(startActivity. java:118) (fragment1.sendData(S1))
I understand that I need to rearrange the lines somewhere, but I don’t understand where.
For those who think that String S = (String) getMapLastIntent().get("taurus") is null, it's not. In another place of the same activity, everything is fine. And checked with String S = (String) ="taurus"; the same error.
Further, with case R.id.nav_gallery: apparently the same problem, the transfer method is different, through intents, it works without error, but simply does not transfer anything.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Создадим новый фрагмент
Fragment fragment = null;
Class fragmentClass = null;
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
FragmentFirst fragment1 = (FragmentFirst)getSupportFragmentManager()
.findFragmentById(R.id.container);
fragmentClass = FragmentFirst.class;
String S1 = (String) getMapLastIntent().get("taurus");
fragment1.sendData(S1);
break;
case R.id.nav_gallery:
/*String S = (String) getMapLastIntent().get("taurus");*/
String S = "taurus";
Intent intent = new Intent();
intent.putExtra("taurus", S);
getApplicationContext().sendBroadcast(intent);
fragmentClass = FragmentSecond.class;
break;
case R.id.nav_slideshow:
break;
case R.id.nav_manage:
break;
case R.id.nav_share:
break;
}
try {
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Вставляем фрагмент, заменяя текущий фрагмент
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
// Выделяем выбранный пункт меню в шторке
item.setChecked(true);
// Выводим выбранный пункт в заголовке
setTitle(item.getTitle());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question