我在Coursera的MOOC上,我正在尝试运行一个读取书签并显示它们的应用程序。
为此,我需要在AndroidManifest中设置权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<!-- TODO - add uses-permission elements -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="@string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="@string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="@string/title_activity_customization" >
</activity>
</application>
</manifest>
这可以防止获得SecurityException,但是,当我尝试在我的Android设备上运行我的应用程序时,我在LogCat中收到以下错误:
03-02 02:58:13.579: I/Lab-Permissions(15438): Entered loadBookmarks()
03-02 02:58:13.589: W/dalvikvm(15438): threadid=1: thread exiting with uncaught exception (group=0x4166d498)
03-02 02:58:13.589: E/ActivityThread(15438): Failed to find provider info for browser
03-02 02:58:13.589: E/test(15438): Exception
03-02 02:58:13.609: E/AndroidRuntime(15438): FATAL EXCEPTION: main
03-02 02:58:13.609: E/AndroidRuntime(15438): java.lang.NullPointerException
03-02 02:58:13.609: E/AndroidRuntime(15438): at course.labs.permissionslab.BookmarksActivity.loadBookmarks(BookmarksActivity.java:59)
03-02 02:58:13.609: E/AndroidRuntime(15438): at course.labs.permissionslab.BookmarksActivity.access$0(BookmarksActivity.java:50)
03-02 02:58:13.609: E/AndroidRuntime(15438): at course.labs.permissionslab.BookmarksActivity$1.onClick(BookmarksActivity.java:32)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.view.View.performClick(View.java:4106)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.view.View$PerformClick.run(View.java:17150)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.os.Handler.handleCallback(Handler.java:615)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.os.Looper.loop(Looper.java:137)
03-02 02:58:13.609: E/AndroidRuntime(15438): at android.app.ActivityThread.main(ActivityThread.java:4792)
03-02 02:58:13.609: E/AndroidRuntime(15438): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:58:13.609: E/AndroidRuntime(15438): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:58:13.609: E/AndroidRuntime(15438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
03-02 02:58:13.609: E/AndroidRuntime(15438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
03-02 02:58:13.609: E/AndroidRuntime(15438): at dalvik.system.NativeStart.main(Native Method)
我不知道发生了什么,似乎它没有找到提供商,但我有两个浏览器,所以我没有得到它。
这是我的BookmarksActivy代码,请忽略调用其他内容的“危险活动”。
package course.labs.permissionslab;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class BookmarksActivity extends Activity {
private static final String TAG = "Lab-Permissions";
static final String[] projection = { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks_activity);
Button getBookmarksButton = (Button) findViewById(R.id.get_bookmarks_button);
getBookmarksButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadBookmarks();
}
});
Button goToDangerousActivityButton = (Button) findViewById(R.id.go_to_dangerous_activity_button);
goToDangerousActivityButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startGoToDangerousActivity();
}
});
}
private void loadBookmarks() {
Log.i(TAG, "Entered loadBookmarks()");
String text = "";
Cursor query = getContentResolver().query(Browser.BOOKMARKS_URI,
projection, null, null, null);
query.moveToFirst();
while (query.moveToNext()) {
text += query.getString(query
.getColumnIndex(Browser.BookmarkColumns.TITLE));
text += "\n";
text += query.getString(query
.getColumnIndex(Browser.BookmarkColumns.URL));
text += "\n\n";
}
TextView box = (TextView) findViewById(R.id.text);
box.setText(text);
Log.i(TAG, "Bookmarks loaded");
}
private void startGoToDangerousActivity() {
Log.i(TAG, "Entered startGoToDangerousActivity()");
Intent dangerous = new Intent(BookmarksActivity.this,GoToDangerousActivity.class);
startActivity(dangerous);
}
}
答案 0 :(得分:2)
您的设备可能会运行Chrome,而不是AOSP浏览器。如果是这样,请使用:
而不是Browser.BOOKMARKS_URIUri.parse("content://com.android.chrome.browser/bookmarks")
即,
Cursor query = getContentResolver().query(Uri.parse("content://com.android.chrome.browser/bookmarks"),
projection, null, null, null);
答案 1 :(得分:0)
我想知道浏览器设置是否与您的问题有关。转到设备上首选浏览器的“应用信息”屏幕。在“默认启动”中点击“清除默认值”按钮,然后查看会发生什么。
答案 2 :(得分:0)
从google play store下载aosp安装程序并安装浏览器。您的设备可能没有Rom附带的默认浏览器。