从手机中检索联系人姓名和号码并在列表视图中显示

时间:2015-11-20 14:09:17

标签: android

该应用程序正在崩溃。我不知道为什么。有没有其他方法可以做到这一点。一切似乎都没问题。

public class MainActivity extends ListActivity {

       ListView lv1;
    Cursor cursor1; 



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

        cursor1 = (Cursor) getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor1);
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
        int[] to = {android.R.id.text1, android.R.id.text2};
        SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to);
        setListAdapter(listadapter);

        lv1 = getListView();
        lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
        }

logcat。以下是运行app的错误。有两个java类main和contactlistfragment。 mainactivity xml文件包含。和contactlistfragment包括listview。

11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime: Process: com.example.bhardwaj.myapplication2, PID: 2043
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:142)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{97d1de7 2043:com.example.bhardwaj.myapplication2/u0a48} (pid=2043, uid=10048) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1552)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:475)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompatJellybean.query(ContentResolverCompatJellybean.java:29)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat$ContentResolverCompatImplJB.query(ContentResolverCompat.java:57)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat.query(ContentResolverCompat.java:125)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:59)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:37)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:296)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:54)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:42)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:128)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
11-21 13:00:43.472 2043-2059/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

主java类有上面的代码和主要的xml包含listview。 新的logcat

11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime: Process: com.example.bhardwaj.myapplication2, PID: 21202
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhardwaj.myapplication2/com.example.bhardwaj.myapplication2.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{af103e9 21202:com.example.bhardwaj.myapplication2/u0a48} (pid=21202, uid=10048) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{af103e9 21202:com.example.bhardwaj.myapplication2/u0a48} (pid=21202, uid=10048) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1552)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:475)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:434)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at com.example.bhardwaj.myapplication2.MainActivity.onCreate(MainActivity.java:51)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-21 15:43:40.130 21202-21202/com.example.bhardwaj.myapplication2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

0 个答案:

没有答案