当前位置: 代码迷 >> Android >> 获取 Android 运行时:致命异常
  详细解决方案

获取 Android 运行时:致命异常

热度:60   发布时间:2023-08-04 10:29:25.0

我正在使用以下联系人获取应用程序代码,我正在获取Android 运行时:致命异常:第 17 行的主要错误。

package com.example.alpesh_pc.contactlist;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Toast;

public class MainActivity extends Activity
{
    public Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cursor=this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if(cursor.getCount()>0)
        {
            Toast.makeText(this, "Seccess", Toast.LENGTH_LONG).show();
        }

    }
}

下面是我的 logcat

E/AndroidRuntime:致命异常:主进程:com.example.alpesh_pc.contactlist,PID:3833 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.alpesh_pc.contactlist/com.example.alpesh_pc.contactlist.MainActivity }: java.lang.SecurityException: Permission Denial: 从 ProcessRecord{6970ad0 3833:com.example.alpesh_pc.contactlist/u0a67} (pid=3833, uid=10067) 中打开提供者 com.android.providers.contacts.ContactsProvider2 需要 android。 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread)的permission.READ_CONTACTS或android.permission.WRITE_CONTACTS .java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154)在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.M ethod.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起: java.lang.SecurityException: Permission Denial: 从 ProcessRecord{6970ad0 3833:com.example.alpesh_pc.contactlist/u0a67} (pid=3833, uid=10067) 中打开提供者 com.android.providers.contacts.ContactsProvider2 需要 android.permission。 READ_CONTACTS 或 android.permission.WRITE_CONTACTS 在 android.os.Parcel.readException(Parcel.java:1683) 在 android.os.Parcel.readException(Parcel.java:1636) 在 android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java: 4169) at android.app.ActivityThread.acquireProvider(ActivityThread.java:5434) at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2267) at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1515) android.content.ContentResolver.query(ContentResolver.java:514) 在和 android.content.ContentResolver.query(ContentResolver.java:472) at com.example.alpesh_pc.contactlist.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6662) at android .app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread。 -wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper. java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit. java:865) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 应用程序终止。

在 Android manifest xml 中添加权限<uses-permission android:name="android.permission.READ_CONTACTS" />

检查 Manifest.xml 中的权限,并为 api 级别 23 或更高级别设置运行时权限,例如:

对于清单文件:

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

和运行时权限检查:

我认为您正在使用 Marshmallow OS(Android 版本 6)或任何需要运行时权限检查的设备。

你试过这个吗?

// 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 explanation 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.
    }
}

并处理权限请求响应

@Override
public void onRequestPermissionsResult(int requestCode,
       String permissions[], int[] grantResults) {
       switch (requestCode) {
          case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
           // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
    }
}

如果仍有任何困惑,你可以看看。

  相关解决方案