应用程序在android studio中的ManagedQuery行中崩溃了

时间:2018-08-18 14:57:17

标签: java android

我已经编写了一个应用程序,其中包含一个显示联系信息的列表。

应用代码如下:

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor mCursor=getContacts();
        startManagingCursor(mCursor);
        ListAdapter adapter=new SimpleCursorAdapter(this,
                android.R.layout.two_line_list_item,
                mCursor, new String[]{ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.DISPLAY_NAME},
                new int[]{android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
    }

    private Cursor getContacts(){
        Uri uri= ContactsContract.Contacts.CONTENT_URI;
        String[] projection=new String[]{ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.DISPLAY_NAME};
        String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "='" + ("1") + "'";
        String[] selectionArgs = null;
        String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor out=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        return out;
    }
}

但是应用程序在managedQuery行崩溃了。

我还向清单文件添加了READ_CONTACTS权限。

怎么了?

已编辑:

这是logcat数据:

--------- beginning of crash
     

08-18 17:36:45.093 6464-6464 / com.example.morteza.listactivitycursor E / Android运行时:致命异常:主要       流程:com.example.morteza.listactivitycursor,PID:6464       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.morteza.listactivitycursor / com.example.morteza.listactivitycursor.MainActivity}:java.lang.SecurityException:权限拒绝:打开提供程序com.android.providers.contacts。来自ProcessRecord {afac265 6464:com.example.morteza.listactivitycursor / u0a98}(pid = 6464,uid = 10098)的ContactsProvider2需要android.permission.READ_CONTACTS或android.permission.WRITE_CONTACTS           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        由以下原因引起:java.lang.SecurityException:权限拒绝:从ProcessRecord {afac265 6464:com.example.morteza.listactivitycursor / u0a98}(pid = 6464,uid = 10098)打开提供程序com.android.providers.contacts.ContactsProvider2需要android .permission.READ_CONTACTS或android.permission.WRITE_CONTACTS           在android.os.Parcel.createException(Parcel.java:1942)           在android.os.Parcel.readException(Parcel.java:1910)           在android.os.Parcel.readException(Parcel.java:1860)           在android.app.IActivityManager $ Stub $ Proxy.getContentProvider(IActivityManager.java:4181)           在android.app.ActivityThread.acquireProvider(ActivityThread.java:5970)           在android.app.ContextImpl $ ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2592)           在android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1828)           在android.content.ContentResolver.query(ContentResolver.java:786)           在android.content.ContentResolver.query(ContentResolver.java:752)           在android.content.ContentResolver.query(ContentResolver.java:710)           在com.example.morteza.listactivitycursor.MainActivity.getContacts(MainActivity.java:35)           在com.example.morteza.listactivitycursor.MainActivity.onCreate(MainActivity.java:17)           在android.app.Activity.performCreate(Activity.java:7136)           在android.app.Activity.performCreate(Activity.java:7127)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        引起原因:android.os.RemoteException:远程堆栈跟踪:           在com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12185)           在com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12582)           在android.app.IActivityManager $ Stub.onTransact(IActivityManager.java:357)           在com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3288)           在android.os.Binder.execTransact(Binder.java:731)   08-18 17:36:45.112 6464-6464 / com.example.morteza.listactivitycursor I / Process:正在发送信号。 PID:6464 SIG:9

怎么了?

1 个答案:

答案 0 :(得分:1)

您必须获得用户的许可。清单不足。
检查以下内容:https://developer.android.com/training/permissions/requesting

更改为此:

    private Cursor getContacts(){
       Uri uri= ContactsContract.Contacts.CONTENT_URI;
       String[] projection=new String[]{ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME};
       String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "= ?";
       String[] selectionArgs = new String[] { "1" };
       String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
       Cursor out=getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
       return out;
   }