我必须编写一个简单的程序,让您从联系人列表中选择一个联系人,并显示其名称,编号和图片。我唯一得到的是一个Cursor错误。我将从LogCat中提供代码和lof。我试图把0而不是 cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)和1而不是cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER),但我得到NullPointerException。有任何想法吗?感谢。
程序:
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.provider.ContactsContract;
public class MainActivity extends Activity {
private static final int PICK_Contact_Request = 0;
TextView textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView1 = (TextView) findViewById(R.id.contact_name);
textView2 = (TextView) findViewById(R.id.contact_phone);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onUpdateContact(View v)
{
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), PICK_Contact_Request);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == PICK_Contact_Request)
{
if(resultCode == RESULT_OK)
{
Cursor cursor = null;
try {
ContentResolver cr = getContentResolver();
Uri uri = data.getData();
cursor = cr.query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(BaseColumns._ID);
int nameIdx = cursor.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME);
int photoIdIdx = cursor.getColumnIndex(CommonDataKinds.Phone.PHOTO_ID);
String name = cursor.getString(nameIdx);
String id = cursor.getString(idx);
int phoneX;
String phoneNumber = null;
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(CommonDataKinds.Phone.CONTENT_URI, null, BaseColumns._ID +" = ?", new String[]{id}, null);
pCur.moveToFirst();
phoneX = pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER);
phoneNumber = pCur.getString(phoneX);
pCur.close();
}
cursor.close();
textView1.setText(name);
textView2.setText(phoneNumber);
img.setImageResource(photoIdIdx);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textSize="20sp"/>
<TextView android:id="@+id/contact_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:layout_below="@id/contact_name"
android:layout_alignLeft="@id/contact_name"/>
<ImageView android:id="@+id/contact_portrait"
android:contentDescription="@string/zdjecie"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#aaa"
android:adjustViewBounds="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/contact_name"
android:layout_marginRight="20dp"/>
<Button android:id="@+id/update_contact_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/updateContact"
android:layout_below="@id/contact_portrait"
android:onClick="onUpdateContact"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"/>
logcat的:
Null Pointer Exception @ line 75: text1.setText(name); and 76 text2.setText(phoneNumber);
答案 0 :(得分:0)
错误可能是尝试使用ContactsContract.CommonDataKinds.Phone.NUMBER
列,因为该列不是此表的一部分,getColumnIndex()将返回-1。需要单独查询此信息(首先检查HAS_PHONE_NUMBER
列的值)。
查看&#34;电话号码&#34;本教程中的部分:http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
<强>更新强> 您的新代码至少存在一些问题: