• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps getting CursorIndexOutOfBoundsException: Index 0 requested

John Foer

Lurker
Mar 7, 2016
4
0
I have a test app where a display a list of articles in a listview. When I click one of the items, I want to display the URI and the title of the article. The URI is displaying fine like this (see attached):



**But the problem is when I try to display the title I get the CursorIndexOutOfBoundsException. It doesn,t like this line of code in my onLoadFinished:**

mTextView.setText(mDetailCursor.getString(3));


I want to display the title and the URI, but I'm getting the following error:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0



I wonder what did I missed?

I tried the suggestions in this post by checking if cursor is not null but it didnt help me display the title:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 6


Here is my DetailFragment class:




public class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{


private Cursor mDetailCursor;
private View mRootView;
private int mPosition;
private ImageView mImageView;
private TextView mTextView;
private TextView mUriText;
private Uri mUri;
private static final int CURSOR_LOADER_ID = 20;


public static DetailFragment newInstance(int position, Uri uri) {
DetailFragment fragment = new DetailFragment();
Bundle args = new Bundle();
fragment.mPosition = position;
fragment.mUri = uri;
args.putInt("id", position);
fragment.setArguments(args);
return fragment;
}

public DetailFragment() {
// Required empty public constructor
}


@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

mTextView = (TextView) rootView.findViewById(R.id.post_title_textvView);
mUriText = (TextView) rootView.findViewById(R.id.uri);
Bundle args = this.getArguments();
getLoaderManager().initLoader(CURSOR_LOADER_ID, args, DetailFragment.this);

return rootView;
}


@override
public void onDetach() {
super.onDetach();
// mListener = null;
}

@override
public Loader<Cursor> onCreateLoader(int id, Bundle args){
if (null != mUri) {

// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(
getActivity(),
mUri,
null,
null,
null,
null
);
}
return null;

}

@override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
}

// Set the cursor in our CursorAdapter once the Cursor is loaded
@override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

mDetailCursor = data;
mDetailCursor.moveToFirst();
DatabaseUtils.dumpCursor(data);

mTextView.setText(mDetailCursor.getString(3));

// set Uri to be displayed
mUriText.setText(mUri.toString());
}

// reset CursorAdapter on Loader Reset
@override
public void onLoaderReset(Loader<Cursor> loader){
mDetailCursor = null;
}

}

Here is my stack trace:





android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.sam_chordas.android.androidflavors.DetailFragment.onLoadFinished(DetailFragment.java:106)
at com.sam_chordas.android.androidflavors.DetailFragment.onLoadFinished(DetailFragment.java:19)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
at android.support.v4.content.Loader.deliverResult(Loader.java:104)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

[1]: http://i.stack.imgur.com/RtVvt.png
 

Attachments

  • URI displaying fine.png
    URI displaying fine.png
    42.5 KB · Views: 258
Hi @LV426
thanks for replying.

here is the output:
I/System.out: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@22b4379

I also added a log in my onLoadIFnished like this:
Log.d("Count",String.valueOf(data.getCount()));

and the output is Count: 0.

The data is definitely present in the database as I can see it in stetho.
 
Upvote 0
HI @LV426 thanks for the suggestion. I already implemented a Content Provider. I now partly solved the problem by changing my onCreateLoader to:

public Loader<Cursor> onCreateLoader(int id, Bundle args){

if (null != mUri) {


// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(
getActivity(),
ArticleContract.ArticleEntry.CONTENT_URI,
null,
null,
null,
null
);
}
return null;


}


now i'm getting a problem whenever i click on the item the title is displaying (which is what I want) but the title never change even if I choose a different item on the list.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones