0

我的应用程序有问题。我正在尝试执行查询,但应用程序每次都停止。尝试执行查询时发生此错误。我正在从 Android Pro 3 书中学习,但本书中提供的代码已被弃用:

package com.example.contactsabuout;

import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.context = getApplicationContext();

        Log.v("INFO", "Completed: onCreate.");
    }

    public static Context getAppContext() {
        return MainActivity.context;
    }

    public void doQuery(View view) {

         Uri peopleBaseUri = ContactsContract.Contacts.CONTENT_URI;
         Log.v("II","Button clicked.");
         Log.v("II", "Uri for ContactsContract.Contacts: " + peopleBaseUri);
         Context context = getAppContext();
         Log.v("II", "Got context: " + context);
         Cursor cur;
         Log.v("II", "Created cursor: cur");
         cur = context.getContentResolver().query(peopleBaseUri, null, null, null, null);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

来自 LogCat

10-28 17:45:02.513: V/INFO(4677): Completed: onCreate.
10-28 17:45:02.613: D/libEGL(4677): loaded /system/lib/egl/libGLES_android.so
10-28 17:45:02.653: D/libEGL(4677): loaded /system/lib/egl/libEGL_adreno200.so
10-28 17:45:02.723: D/libEGL(4677): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
10-28 17:45:02.723: D/libEGL(4677): loaded /system/lib/egl/libGLESv2_adreno200.so
10-28 17:45:03.014: I/Adreno200-EGLSUB(4677): <ConfigWindowMatch:2078>: Format RGBA_8888.
10-28 17:45:03.054: D/OpenGLRenderer(4677): Enabling debug mode 0
10-28 17:45:03.254: D/OpenGLRenderer(4677): has fontRender patch
10-28 17:45:03.274: D/OpenGLRenderer(4677): has fontRender patch
10-28 17:45:12.873: V/II(4677): Button clicked.
10-28 17:45:12.873: V/II(4677): Uri for ContactsContract.Contacts: content://com.android.contacts/contacts, rest will be null
10-28 17:45:12.873: V/II(4677): Got context: android.app.Application@40d83d90
10-28 17:45:12.873: V/II(4677): Created cursor: cur
10-28 17:45:12.933: D/AndroidRuntime(4677): Shutting down VM
10-28 17:45:12.933: W/dalvikvm(4677): threadid=1: thread exiting with uncaught exception (group=0x40aaf228)
10-28 17:45:12.953: E/AndroidRuntime(4677): FATAL EXCEPTION: main
10-28 17:45:12.953: E/AndroidRuntime(4677): java.lang.IllegalStateException: Could not execute method of the activity
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.view.View$1.onClick(View.java:3071)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.view.View.performClick(View.java:3538)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.view.View$PerformClick.run(View.java:14330)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.os.Handler.handleCallback(Handler.java:608)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.os.Looper.loop(Looper.java:156)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.app.ActivityThread.main(ActivityThread.java:4977)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invoke(Method.java:511)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at dalvik.system.NativeStart.main(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677): Caused by: java.lang.reflect.InvocationTargetException
10-28 17:45:12.953: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invoke(Method.java:511)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.view.View$1.onClick(View.java:3066)
10-28 17:45:12.953: E/AndroidRuntime(4677):     ... 11 more
10-28 17:45:12.953: E/AndroidRuntime(4677): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts from pid=4677, uid=10155 requires android.permission.READ_CONTACTS
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.os.Parcel.readException(Parcel.java:1332)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:406)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at android.content.ContentResolver.query(ContentResolver.java:315)
10-28 17:45:12.953: E/AndroidRuntime(4677):     at com.example.contactsabuout.MainActivity.doQuery(MainActivity.java:47)
10-28 17:45:12.953: E/AndroidRuntime(4677):     ... 14 more

我正在努力学习安卓。

4

2 回答 2

1

在清单文件中添加以下权限。

android.permission.READ_CONTACTS

于 2012-10-28T16:57:42.963 回答
0

LogCat 中的这一行:

引起:java.lang.SecurityException: Permission Denial: 从 pid=4677 读取 com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts, uid=10155 需要 android.permission.READ_CONTACTS

通知您需要添加:

<uses-permission android:name="android.permission.READ_CONTACTS" />

到您的清单。


将其添加到<manifest>元素中,与<application>or相同<uses-sdk>

<manifest>

    <uses-sdk />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <application />

</manifest>
于 2012-10-28T16:58:52.907 回答