-1

In my application I am getting Null Pointer Exception when click on the up action. The navigation happening properly to parent activity. And it going to the onCreate method of the parent activity. It is expecting a value from intent and it coming as null. But not sure how to set an intent value on up action pressed. Any help appreciated.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference

activity 1:

public class SignalListActivity extends AppCompatActivity {

public static final String EXTRA_CAT_ID = "sigcatid";
private DatabaseQuery dbQuery;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signal_list);
    setTitle("Cat chat");
    try {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(mToolbar);

        if(getSupportActionBar() != null){
            getSupportActionBar().setTitle("Signal List");

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        dbQuery = new DatabaseQuery(SignalListActivity.this);
        final String cat = (String) getIntent().getExtras().get(EXTRA_CAT_ID);
        List<Signal> signals = dbQuery.getSignalsNew(cat);
        //getting recycler view

        RecyclerView signalRecycler =(RecyclerView)findViewById(R.id.signal_recycler);
        //GridLayoutManager mGrid = new GridLayoutManager(this, 2);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);


        signalRecycler.setLayoutManager(layoutManager);
       // signalRecycler.setHasFixedSize(true);

        SignalAdapter mAdapter = new SignalAdapter(SignalListActivity.this, signals);
        signalRecycler.setAdapter(mAdapter);
        mAdapter.setListener(new SignalAdapter.Listener() {
            @Override
            public void onClick(long id) {
                Intent intent = new Intent(getApplicationContext(),SignalActivity.class);


                intent.putExtra(SignalActivity.EXTRA_SIG_ID, id);
                intent.putExtra(EXTRA_CAT_ID, cat);
                startActivity(intent);
            }

            /*public void onClick(int position) {
                Intent intent = new Intent(this, SignalActi.class);
                intent.putExtra(PizzaDetailActivity.EXTRA_PIZZA_ID, position);
                getActivity().startActivity(intent);
            }*/
        });





    } catch(SQLiteException e) {
        Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
        toast.show();
    }

.....

Activity 2

public class SignalActivity extends AppCompatActivity {

public static final String EXTRA_SIG_ID = "signalId";
private DatabaseQuery dbQuery;
private Context context;
private String cat;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signal);
    dbQuery = new DatabaseQuery(SignalActivity.this);
    Toolbar mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(mToolbar);
    cat = (String) getIntent().getExtras().get(EXTRA_CAT_ID);
    if(getSupportActionBar() != null){
        getSupportActionBar().setTitle("Signal Details");

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    try {


        Long signalId = (Long) getIntent().getExtras().get(EXTRA_SIG_ID);

        Signal signal = dbQuery.getSignal(signalId);
        TextView name = (TextView)findViewById(R.id.name);
        name.setText(signal.getName());

        //Populate the drink description
        TextView description = (TextView)findViewById(R.id.description);
        description.setText(signal.getDescription());

        TextView teaser = (TextView)findViewById(R.id.teaser);
        teaser.setText(signal.getTeaser());

        //Populate the drink image
        ImageView photo = (ImageView)findViewById(R.id.photo);
        int photoId =  getResourseId(this,signal.getImage(), "drawable",getPackageName());

        photo.setImageResource(photoId);
        photo.setContentDescription(signal.getName());

    } catch(SQLiteException e) {
        Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
        toast.show();
    }

... when clicking up button from activity 2 it is going to onCreate method of activity 1 and expecting the final String cat = (String) getIntent().getExtras().get(EXTRA_CAT_ID);

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shams
  • 557
  • 8
  • 15

2 Answers2

1

If you have 2 activities

  1. Activity A
  2. Activity B

Suppose you now in Activity B, when you press the back button, to navigate back to Activity A, your lifecycle in Activity A would enter onResume(), NOT onCreate() as your Activity A still in you back stack, it doesn't require to recreate the activity once again. Hence you getting null values, as you are expecting onCreate() callback, which unfortunately won't happen.

To solve this problem we have 3 options:

1) Add a SharedViewModel for both activities, use a MutableLiveData in this ViewModel, add data in Activity B, and observe these data in Activity A.

2) Use Broadcast receiver or callback interface between 2 activities.

3) Start new Intent from Activity B to Activity A and receive the data in onCreate()

MustafaKhaled
  • 1,343
  • 9
  • 25
1

Change these two lines:

1)

getIntent().getStringExtra(EXTRA_CAT_ID);

instead of

getIntent().getExtras().get(EXTRA_CAT_ID);

2)

getIntent().getLongExtra(EXTRA_SIG_ID,0);

instead of

getIntent().getExtras().get(EXTRA_SIG_ID); 

Because of getIntent().getExtras() is null

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35