在我的应用程序中,我想获取浏览器历史记录,我尝试使用以下代码,但它抛出异常,下面是代码:
private static final Uri BOOKMARKS_URI = null;
private static final String[] HISTORY_PROJECTION = null;
private static final int HISTORY_PROJECTION_TITLE_INDEX = 0;
private static final int HISTORY_PROJECTION_URL_INDEX = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl(turl);
mWebView.setWebViewClient(new BrowserClient() {
public void onPageFinished(WebView view, String url) {
getBrowserHist();
}
});
}
public void getBrowserHist() {
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
}
但该程序抛出此异常:
12-27 11:50:55.122: ERROR/AndroidRuntime(295): FATAL EXCEPTION: main
12-27 11:50:55.122: ERROR/AndroidRuntime(295): java.lang.NullPointerException
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.content.ContentResolver.acquireProvider(ContentResolver.java:727)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.content.ContentResolver.query(ContentResolver.java:239)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.app.Activity.managedQuery(Activity.java:1520)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at com.mobila.browser.Browser.getBrowserHist(Browser.java:108)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at com.mobila.browser.Browser$1.onPageFinished(Browser.java:70)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:274)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.os.Looper.loop(Looper.java:123)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at java.lang.reflect.Method.invoke(Method.java:521)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-27 11:50:55.122: ERROR/AndroidRuntime(295): at dalvik.system.NativeStart.main(Native Method)
所以,如果有人已经使用此代码来阅读浏览器历史记录,请告诉我plz我错在哪里,或者任何帮助..
由于
答案 0 :(得分:1)
您需要适当的权限才能阅读浏览器历史记录:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />