2011-12-27 126 views
0

在我的應用程序,我想獲得瀏覽器歷史記錄,我嘗試用下面的代碼,但它拋出一個異常,下面是代碼:致命異常

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

好吧,我知道了,問題是,它認爲,「瀏覽器」是我自己的類,所以當我把「android.provider.Browser 「而不是」getBrowserHist()「中的」瀏覽器「功能,它工作! – 2011-12-27 20:10:46

回答

1

你需要一個適當的權限讀取瀏覽器的歷史:

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> 
+0

感謝您的回覆,但我已經設置了所需的權限 – 2011-12-27 19:20:52