在列表视图中填充电话联系人时出错

Posted

技术标签:

【中文标题】在列表视图中填充电话联系人时出错【英文标题】:Error while Populating phone contacts in a listview 【发布时间】:2015-05-31 02:10:57 【问题描述】:

我正在尝试在自定义列表视图中填充我的联系人列表 但我面临这个问题

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ListView
    android:layout_
    android:layout_
    android:id="@+id/listView"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true" />

listview_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_
android:layout_>

<LinearLayout
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:layout_gravity="right"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="20dp"
    android:paddingTop="30dp">

    <TextView
        android:layout_
        android:layout_
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/name" />

    <TextView
        android:layout_
        android:layout_
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/num" />
</LinearLayout>

我的主要活动课

公共类 MainActivity 扩展 ActionBarActivity

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

    ListView lv = (ListView) findViewById(R.id.listView);

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
     String [] cols = ContactsContract.Contacts._ID
             , ContactsContract.Contacts.DISPLAY_NAME
             , ContactsContract.Contacts.LOOKUP_KEY
             ,ContactsContract.Contacts.HAS_PHONE_NUMBER;

    String selections = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ? ";

    Cursor cursor = getContentResolver().query(uri,cols,selections,new String [] "1",null);

    Adapter adapter = new Adapter(MainActivity.this , cursor);
    lv.setAdapter(adapter);


扩展CursorAdapter的适配器类

类适配器扩展 CursorAdapter

    Context context;
    Cursor c;

    public Adapter(Context context , Cursor c ) 
        super(context, c, 0);
        this.context = context;
        this.c = c;
    

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) 
        View v = getLayoutInflater().inflate(R.layout.listview_item, viewGroup, false);

        return v;
    

    @Override
    public void bindView(View view, Context context, Cursor cursor) 


            TextView name = (TextView) findViewById(R.id.name);
            name.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            TextView num = (TextView) findViewById(R.id.num);
            num.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));

        
    

这里是 LogCat 异常

03-26 20:56:55.450    1847-1847/com.examples.anabil.contentproviderex E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.examples.anabil.contentproviderex.MainActivity$Adapter.bindView(MainActivity.java:79)
        at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
        at android.widget.AbsListView.obtainView(AbsListView.java:2177)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
        at android.widget.ListView.onMeasure(ListView.java:1159)
        at android.view.View.measure(View.java:15848)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

您是否在 XML 中声明了 ID 为 namenum 的 TextView 项目?另外,尝试更改bindView() 方法:TextView name = (TextView) view.findViewById(R.id.name); 对不起,我忘了复制 xml 代码,但我最近做了 您是否尝试更改 bindView()method: TextView name = (TextView) view.findViewById(R.id.name);(第二个 textView 也一样)? 是的!它有效,但为什么它在 num textview 中只给我数字 1 因为你在查询它:getContentResolver().query(uri,cols,selections,new String [] "1",null); 【参考方案1】:

VadymVL 答案将修复您的空指针错误。

访问原始联系人表 (ContactsContract.Contacts) 很慢。尝试使用 ContactsContract.CommonDataKinds.Phone:

public Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String [] cols = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
         , ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
         , ContactsContract.CommonDataKinds.Phone.NUMBER; 

String selections =  ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + " = '1'";

Cursor Cursor = contentResolver.query(uri, cols, selections, null, null);

在绑定视图中:

@Override
public void bindView(View view, Context context, Cursor cursor) 

        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        TextView num = (TextView) view.findViewById(R.id.num);
        num.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    

【讨论】:

谢谢,但是 Contacts 类和 CommonDataKinds 类有什么不同呢? Contacts - 有原始联系方式,不包含电话号码、电子邮件、昵称等数据。您需要查询 ContactsContract.Data 以获取这些详细信息。使用 CommonDataKinds.Phone 或 CommanDataKinds.Email 之类的数据类型来查询您想要的特定数据更简单。 android

以上是关于在列表视图中填充电话联系人时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 SWIFT 的表视图中填充自定义单元格时出错

有没有办法用 2.3 的联系人照片填充列表视图? [复制]

联系人在列表视图的一行中列出?

安卓。将联系人显示为列表视图

如何使用 EditText 在列表视图中搜索联系人?

从联系人意图添加联系人后如何刷新列表视图?