当我使用Intent开始活动时,我的应用程序已停止工作

时间:2019-06-12 13:17:39

标签: java android

当我从另一个活动开始getContact活动时,应用程序已停止。当我在mainActivity中编写代码时,getContact活动工作正常,这意味着不使用意图将起作用。我也尝试了很多解决方案,请帮助我。

这是getContact活动

package com.example.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class getContact extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //Bundle extras=getIntent().getExtras();
        //String name=extras.getString("ContactName");
        //String SenderNum=extras.getString("SenderNum");
        String name="Name";
        String number=get(name,getContact.this);
        Toast.makeText(getApplicationContext(), "Number "+number, Toast.LENGTH_SHORT).show();
    }

    public String get(String name, Context context) {
        String ret=null;
        String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
        String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, selection, null, null);
        if (c.moveToFirst()) {
            ret = c.getString(0);
        }
        c.close();
        if(ret==null)
            ret = "Unsaved";
        return ret;
    }
}

并且我尝试使用其他活动中的以下内容开始以上活动

 Intent i=new Intent(contactActivity.this,getContact.class);
                startActivity(i);
                finish();

这是我开始活动时logcat的输出

    --------- beginning of crash
2019-06-12 09:26:17.399 6361-6361/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 6361
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.getContact}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{386669f 6361:com.example.myapplication/u0a335} (pid=6361, uid=10335) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6238)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{386669f 6361:com.example.myapplication/u0a335} (pid=6361, uid=10335) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
        at android.os.Parcel.readException(Parcel.java:1718)
        at android.os.Parcel.readException(Parcel.java:1671)
        at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4267)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:5579)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2280)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1533)
        at android.content.ContentResolver.query(ContentResolver.java:518)
        at android.content.ContentResolver.query(ContentResolver.java:475)
        at com.example.myapplication.getContact.get(getContact.java:27)
        at com.example.myapplication.getContact.onCreate(getContact.java:19)
        at android.app.Activity.performCreate(Activity.java:6857)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:163) 
        at android.app.ActivityThread.main(ActivityThread.java:6238) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

3 个答案:

答案 0 :(得分:1)

查看错误日志后,它显示安全异常,我相信未授予权限。

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

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

此外,如果您要定位Marshamallow以上的设备,则需要考虑授予用户权限。

 requestPermissions(new String[]{READ_CONTACTS, WRITE_CONTACTS},PERMISSION_REQUEST_CODE);

检查权限是否已通过

授予
 int result3 = ContextCompat.checkSelfPermission(getApplicationContext(), READ_CONTACTS);
 int result4 = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_CONTACTS);

两个值都应为0

答案 1 :(得分:0)

您不会夸大您活动的布局。您的onCreate方法应调用setContentView(R.layout.layout_name)

答案 2 :(得分:0)

如果您删除了这一行,则添加并显示在应用程序崩溃时Logcat中出现的错误。...

确保:

  1. setContentView(R.layout.xxxxxxx);
  2. 不要忘记在清单中声明活动。