Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

一个人想着一个人 提交于 2019-11-28 01:52:40

The array in your ArrayAdapter contains at least one entry that is null. There must be no nulls there.

The array is populated in getDiaryDBDataList() so the problem is also there.

I guess instead of

String selectedEntry = someTextView.getText().toString();

you have to use

String selectedEntry = listAdapter.getItem(position);

Because you are using array adapter you can't get selected item from View

Hope this will help you out

Initialize "db_data_list" in displayItems()

ArrayList db_data_list = new ArrayList();

view as you have so named it here appears to be uninitialized and not tied to anything

Textview someTextView = (TextView) data_list.findViewById(R.id.mytextview);

^ assuming the textview is inside of the data_list view, otherwise discard that bit

Then, in your code, you will want to also add length checks for

 // Selected item store 
            String selectedEntry = someTextView.getText().toString();
            //Check length before trying to split anything
            if (selectedEntry.length() > 0){
            // Test for regular expression 
            String[] listViewItemSplit = selectedEntry.split(" - ");
            String listViewItempt1 = listViewItemSplit[0]; // For date and time

And this will help with issues down the road.

Late answer but, n my case, I changed the resources tag name from integer-array to string-array and it worked fine.

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