我能够成功获得最后一次访问" url"使用下面的功能。
问题 - 我想在上次访问最后一个URL时获取时间条目。试图获得如下所示的 dateTime 会导致程序崩溃。
dateTime = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.DATE));
我失败的堆栈跟踪也在下面。感谢您对我如何解决这个问题的任何指示?
public String returnLastChromeURL(int browserCode) {
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
String dateTime;
Uri uriCustom = Uri
.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
// 1 = bookmark
Cursor mCur = mContext.getContentResolver().query(uriCustom, proj, sel,
null, BookmarkColumns.DATE + " ASC");
mCur.moveToFirst();
mCur.moveToLast();
dateTime = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.DATE));
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
Log.v("focus" + " url", "Chrome URL = " + url + ", dateTime = " + dateTime);
return url;
}
以下是我的Stack Trace失败:
05-02 02:29:33.325: E/CursorWindow(10382): Failed to read row 1656, column -1 from a CursorWindow which has 1657 rows, 2 columns.
05-02 02:29:33.336: E/AndroidRuntime(10382): FATAL EXCEPTION: main
05-02 02:29:33.336: E/AndroidRuntime(10382): java.lang.IllegalStateException: Couldn't read row 1656, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.database.CursorWindow.nativeGetString(Native Method)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.database.CursorWindow.getString(CursorWindow.java:438)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-02 02:29:33.336: E/AndroidRuntime(10382): at com.mavdev.focusoutfacebook.urls.MyURLObject.returnLastChromeURL(MyURLObject.java:71)
05-02 02:29:33.336: E/AndroidRuntime(10382): at com.mavdev.focusoutfacebook.FBBlockerService$FBServicelockCountdownTimer.onTick(FBBlockerService.java:291)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.os.Looper.loop(Looper.java:137)
05-02 02:29:33.336: E/AndroidRuntime(10382): at android.app.ActivityThread.main(ActivityThread.java:5455)
05-02 02:29:33.336: E/AndroidRuntime(10382): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 02:29:33.336: E/AndroidRuntime(10382): at java.lang.reflect.Method.invoke(Method.java:525)
05-02 02:29:33.336: E/AndroidRuntime(10382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-02 02:29:33.336: E/AndroidRuntime(10382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-02 02:29:33.336: E/AndroidRuntime(10382): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的预测也应包含所需的列名。所以改变代码
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
到
String[] proj = new String[] { Browser.BookmarkColumns.DATE, Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
希望这有帮助。