Android getIntent().getExtras() returning null sometimes

不打扰是莪最后的温柔 提交于 2019-12-07 05:01:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!