使用意图

时间:2017-05-30 08:48:37

标签: android android-intent android-contentprovider

我正在学习Android开发课程,因此我对这一切都很新。我需要通过intent获取联系人,获取联系人的ID和名称并将其显示在ListView中。
我试图按照我的课程说明,但我无法让它工作。当我点击“邀请某人”按钮时它会崩溃。

这是Actiivty:

    <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 Monitor中得到了这个消息:

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)

1 个答案:

答案 0 :(得分:0)

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

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