I was going through Crashlytics logs for my app and found that there is a NullPointerException on some devices because getIntent().getExtras() is returning null.
This behavior is seen only on a few devices and I am not able to reproduce this bug. I know that I can check in the activity if the Bundle is null and prevent the NullPointerException but I was more interested in finding out the root cause for this behavior.
Edit:
Source -
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) parent.getAdapter().getItem(position);
Intent intent = new Intent(getActivity(), SelectedTopicActivity.class);
intent.putExtra("topicId", c.getLong(c.getColumnIndex(AppContract.TopicsEntry._ID)));
startActivity(intent);
}
});
Destination -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selected_topic);
Bundle bundle = getIntent().getExtras();
topicId = bundle.getLong("topicId");
...
}
If the cursor were null, I would have got the error in the source itself. I don't think the field in the cursor is null, otherwise the item won't be there in the list in the first place.
来源:https://stackoverflow.com/questions/38498499/android-getintent-getextras-returning-null-sometimes