Uri 引起的 CursorIndexOutOfBoundsException

发布于 2024-09-16 15:04:32 字数 1222 浏览 1 评论 0原文

这是我的 LogCat 日志:

08-25 22:35:27.989: ERROR/AndroidRuntime(327): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3 

这是获取光标的 onCreate(Bundle) 方法:

private static final String[] PROJECTION = { 
    Formulas._ID, Formulas.TITLE, Formulas.FORMULA 
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri uri = getIntent().getData();

    Cursor formulaCursor = managedQuery(uri, PROJECTION, null, null, null);

    String formulaName = formulaCursor.getString(formulaCursor.getColumnIndex(Formulas.TITLE));
    String formula = formulaCursor.getString(formulaCursor.getColumnIndex(Formulas.FORMULA));

    ((TextView) findViewById(R.id.formula)).setText(formula);
    ((TextView) findViewById(R.id.formula_name)).setText(formulaName);
}

这是我实际启动活动的位置:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Uri formula = ContentUris.withAppendedId(getIntent().getData(), id);
    startActivity(new Intent(Intent.ACTION_VIEW, formula));
}

我已经有了自己的内容提供程序,并且正确设置了它。

Here is my LogCat log:

08-25 22:35:27.989: ERROR/AndroidRuntime(327): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3 

Here is the onCreate(Bundle) method where I get the cursor:

private static final String[] PROJECTION = { 
    Formulas._ID, Formulas.TITLE, Formulas.FORMULA 
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri uri = getIntent().getData();

    Cursor formulaCursor = managedQuery(uri, PROJECTION, null, null, null);

    String formulaName = formulaCursor.getString(formulaCursor.getColumnIndex(Formulas.TITLE));
    String formula = formulaCursor.getString(formulaCursor.getColumnIndex(Formulas.FORMULA));

    ((TextView) findViewById(R.id.formula)).setText(formula);
    ((TextView) findViewById(R.id.formula_name)).setText(formulaName);
}

And here is where I actually start the activity:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Uri formula = ContentUris.withAppendedId(getIntent().getData(), id);
    startActivity(new Intent(Intent.ACTION_VIEW, formula));
}

I already have my own content provider and I set it up properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-09-23 15:04:32

您没有执行formulaCursor.moveToFirst()

You didn't do formulaCursor.moveToFirst()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文