【问题标题】:Android contact content provider using intent使用意图的 Android 联系内容提供者
【发布时间】:2017-05-30 08:48:36
【问题描述】:

我正在学习 Android 开发课程,因此我对这一切还很陌生。我需要通过意图获取联系人,获取联系人的 ID 和姓名并将其显示在 ListView 中。
我试图按照课程中的说明进行操作,但我无法让它发挥作用。当我点击“邀请某人”按钮时它崩溃了。

这是活动:

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Invite someone"
        android:onClick="pickContact"/>

    <TextView
        android:id="@+id/contactTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="contact here" />

这是相应java文件中的代码:

public class Event extends AppCompatActivity {

TextView myContactTextView;
private static int PICK_CONTACT_REQ = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event);

    myContactTextView = (TextView) findViewById(R.id.contactTextView);
}


public void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQ);

    onActivityResult();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQ) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.
            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            try {
                myContactTextView.setText("Your device has these contacts");

                if (cursor.getCount() > 0) {
                    int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                    int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

                    while (cursor.moveToNext()) {
                        int id = cursor.getInt(idColumn);
                        String contactName = cursor.getString(nameColumn);

                        myContactTextView.append(("\n" + id + "-" + contactName));
                    }
                }
            } finally {
                cursor.close();
            }
        }
    }
}
}

有人可以帮忙吗?

编辑:我在 Android 监视器中收到此消息:

05-30 11:05:30.539 18746-18746/com.company.reds.awesomeapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.company.reds.awesomeapp, PID: 18746
                                                                         java.lang.IllegalStateException: Could not find method pickContact(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                             at android.view.View.performClick(View.java:5207)
                                                                             at android.view.View$PerformClick.run(View.java:21168)
                                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

【问题讨论】:

  • 定义:它崩溃了(就像程序员而不是用户)
  • 不确定您是否在寻找什么,但我在原始帖子中添加了来自 Android 监视器的消息。应用程序关闭,并弹出旧的“不幸的是“应用程序”已停止”消息。
  • 我在想logcat的日志... Exception is self-explanatory

标签: android android-intent android-contentprovider


【解决方案1】:

我自己解决了。 我忘记将视图视图添加到我的意图中

编辑:下面提到我将 View 视图添加到 pickContact() 方法而不是意图。

【讨论】:

  • 我忘记将 View view 添加到我的意图中 ...而不是 View view 作为 pickContact 方法中的参数
  • 是的,你的权利。我对我的答案进行了编辑。我仍然有问题,但我不知道为此打开一个新问题是否是一种好习惯?
  • 首先阅读新的异常... 然后尝试用谷歌搜索它 ...如果您找不到类似的问题(并且异常/问题与这个问题)然后问另一个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
相关资源
最近更新 更多