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);