【问题标题】:Pick Contacts onClick when Button is pressed Android按下按钮时选择联系人onClick Android
【发布时间】:2016-01-17 03:28:09
【问题描述】:

我的应用有问题。我想做的是当我的按钮被按下以打开我的电话联系人并将它们显示到我的 TextViews 时。我认为在执行 Button Press 时遇到问题。

至少 LogCat 说我的 Button 出现了一些错误,但我不知道出了什么问题。

在我的清单文件中,我已经设置了读写联系人的权限。我有一个发生代码的 ContactView.java,并且我已经使用 Button 和 2 个 textView 创建了视图。

也许有人可以帮助我,或者至少告诉我我做错了什么。

在我看来,我说:android:onClick="pickContact"

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85500;
private TextView textView1;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    textView1 = (TextView) findViewById(R.id.TxtName);
    textView2 = (TextView) findViewById(R.id.TxtNumber);
}
public void pickContact(View v)
{
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // check whether the result is ok
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be usign multiple startActivityForReslut
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("ContactView", "Failed to pick contact");
    }
}
/**
 * Query the Uri and read contact details. Handle the picked contact data.
 * @param data
 */
private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我在调试时按下 addcontacts 按钮后 LogCat 所说的:

    10-18 23:11:08.684    4353-4353/me.resq.resqme E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: me.resq.resqme, PID: 4353
        java.lang.IllegalStateException: Could not execute method of the activity
                at android.view.View$1.onClick(View.java:3969)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.view.View$1.onClick(View.java:3964)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
                at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:746)
                at me.resq.resqme.ContactView.pickContact(ContactView.java:28)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.view.View$1.onClick(View.java:3964)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: java android contacts contactpicker


    【解决方案1】:
    private static final int RESULT_PICK_CONTACT = 85500;
    

    请尝试将其更改为更低的内容,例如:

    private static final int RESULT_PICK_CONTACT = 85;
    

    【讨论】:

    • 实际工作...这是摆脱这个问题的正确方法吗?
    • @idunnowhatimdoin 我没有阅读你的整个堆栈跟踪,我的答案是错误的,所以我删除了它。关于这里发生的事情的更多解释:stackoverflow.com/questions/25529865/…
    猜你喜欢
    • 2012-11-06
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多