0

我的问题是什么?我正在尝试在对话中创建一个自定义适配器。我不明白错误的原因是什么。

创建对话框:

  protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle("Choose contact");

    view = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog, null);
    adb.setView(view);

    deditText = (EditText) view.findViewById(R.id.deditText);
    deditText.addTextChangedListener(this);
    dlvNames = (ListView) view.findViewById(R.id.dlvCheck);

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] { Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER },
            null, null, null);

      startManagingCursor(cursor);


     while (cursor.moveToNext()) {
         ID=i;
         CID=Long.valueOf(cursor.getString(0));
         NAME=cursor.getString(1);
         PHONE=cursor.getString(2);

         Contact_List.add(new Contact(ID,CID,NAME,PHONE));

      i++;
     }

    contactAdapter = new ContactAdapter(this,Contact_List );
    //crashes here
    dlvNames.setAdapter(contactAdapter);

    dialog = adb.create();
    return dialog;
}

创建自定义适配器:

    public class ContactAdapter extends ArrayAdapter<Contact> {

private final Activity context;
ArrayList<Contact> Contact_List;
LayoutInflater lInflater;


static class ViewHolder {
      public TextView tvName;
      public TextView tvPhone;

    }
public ContactAdapter(Activity context, ArrayList<Contact> Contact_List) {

    super(context, R.layout.item, Contact_List);
    this.context = context;
    this.Contact_List = Contact_List;
}

 public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = context.getLayoutInflater();

      //view = lInflater.inflate(R.layout.item, parent, false);

        view = inflater.inflate(R.layout.item, null, true);

      holder = new ViewHolder();
      holder.tvName = (TextView) view.findViewById(R.id.tvName);
      holder.tvPhone = (TextView) view.findViewById(R.id.tvPhone);
      convertView.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    Contact c = getContact(position);

    holder.tvName.setText(c.name);
    holder.tvPhone.setText(c.phone);


    return view;
}
  Contact getContact(int position) {
        return ((Contact) getItem(position));
      }
}

日志猫:

    01-17 06:40:31.801: E/AndroidRuntime(1263): FATAL EXCEPTION: main
    01-17 06:40:31.801: E/AndroidRuntime(1263): java.lang.NullPointerException
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at com.example.testreadcontacts.ContactAdapter.getView(ContactAdapter.java:50)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.AbsListView.obtainView(AbsListView.java:2271)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.ListView.onMeasure(ListView.java:1156)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.View.measure(View.java:15172)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.Choreographer.doFrame(Choreographer.java:525)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.os.Handler.handleCallback(Handler.java:615)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.os.Handler.dispatchMessage(Handler.java:92)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.os.Looper.loop(Looper.java:137)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at java.lang.reflect.Method.invoke(Method.java:511)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-17 06:40:31.801: E/AndroidRuntime(1263):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2
  convertView.setTag(holder);

这是在检查 null 的块内。如果您点击这一行,则该变量被保证为空。您希望它改为 view.setTag 。

于 2013-01-17T07:17:14.393 回答
0

我的猜测是联系人可能为空,请尝试:

Contact c = getContact(position);
if(c != null) {
   holder.tvName.setText(c.name);
   holder.tvPhone.setText(c.phone);
}
于 2013-01-17T07:15:01.587 回答