应用已经停止,不知道为什么

时间:2016-03-26 15:30:07

标签: android

这只是一个简单的应用程序,其中包含一个获取联系人详细信息的按钮 ,,这是我的按钮代码,我添加了权限,但我的应用程序停止,只是给我一些提示可能是什么问题,我不能发布问题,除非我添加一些细节,所以我试图把更多的话

public void GetContacts(View view) {

    Cursor cursor=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
    while (cursor.moveToNext())
    {
        int nameIndex=cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        String name= cursor.getString(nameIndex);
        Log.d("mainActivityContacts",name);
    }
}

这里也是

的日志猫
03-26 16:41:21.794 5019-5019/com.example.hp250.provider E/AndroidRuntime:   
FATAL EXCEPTION: main
  Process: com.example.hp250.provider, PID: 5019
  java.lang.IllegalStateException: Could not execute method for android:onClick
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
      at android.view.View.performClick(View.java:5198)
      at android.view.View$PerformClick.run(View.java:21147)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invoke(Native Method)
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
      at android.view.View.performClick(View.java:5198) 
      at android.view.View$PerformClick.run(View.java:21147) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
   Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{b0dfc36 5019:com.example.hp250.provider/u0a75} (pid=5019, uid=10075) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
      at android.os.Parcel.readException(Parcel.java:1599)
      at android.os.Parcel.readException(Parcel.java:1552)
      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
      at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
      at android.content.ContentResolver.query(ContentResolver.java:475)
      at android.content.ContentResolver.query(ContentResolver.java:434)
      at com.example.hp250.provider.MainActivity.GetContacts(MainActivity.java:21)
      at java.lang.reflect.Method.invoke(Native Method) 
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
      at android.view.View.performClick(View.java:5198) 
      at android.view.View$PerformClick.run(View.java:21147) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:0)

从清单文件发布您的权限部分 - 看起来您没有添加异常中提到的一些权限:

requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTSn 

如果您尝试清理项目并再次构建

答案 1 :(得分:0)

我猜您的目标是SDK 23,您需要添加Requesting Permissions at Run Time

例如:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an expanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}