我試圖在Dialog
中添加自定義ListView
。我的代碼如下:對話框中的自定義列表視圖
dialog = new Dialog(MyWallet.this);
dialog.setContentView(R.layout.emergency_contacts);
dialog.setTitle("Emergency Contacts");
dialog.show();
mAdapter.addInfo("Name");
ListView lv = (ListView) dialog.findViewById(android.R.id.list);
if (lv != null) {
lv.setAdapter(mAdapter);
}
,然後我的自定義Adapter
:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_INFO = 1;
private static final int TYPE_MAX_COUNT = 1;
private LayoutInflater mInflater;
private int noteFull = 0;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addInfo(final String info) {
mInfo.add(info);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mInfo.size();
}
@Override
public String getItem(int position) {
return mInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return TYPE_INFO;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_INFO:
convertView = mInflater.inflate(R.layout.emergency_contacts_list, null);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.name.setText(mInfo.get(position));
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
switch(type) {
case TYPE_INFO:
convertView = mInflater.inflate(R.layout.emergency_contacts_list, null);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.name.setText(mInfo.get(position));
break;
}
}
return convertView;
}
}
public static class ViewHolder {
public TextView name;
public TextView phoneNumber;
public TextView emailAddress;
}
代碼崩潰(與NullPointerException
在
holder.name =(TextView的)convertView.findViewById( R.id.name);
我在猜,因爲convertView
不是正確的看法,我該如何設置它的權利View
?
編輯:Code for emergency_contacts_list
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Name">
</TextView>
<TextView
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Phone Number">
</TextView>
<TextView
android:id="@+id/email"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Email Address">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:gravity="right|center_vertical"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/call"
android:layout_height="35px"
android:layout_width="50px"
android:background="@drawable/custom_button"
android:layout_margin="3px"
android:text="Call">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
編輯2:全logcat
:
03-19 15:39:43.157: ERROR/AndroidRuntime(23371): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.intellimec.MyWallet$MyCustomAdapter.getView(MyWallet.java:207)
at android.widget.AbsListView.obtainView(AbsListView.java:1428)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
at android.widget.ListView.onMeasure(ListView.java:1128)
at android.view.View.measure(View.java:8524)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8524)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8524)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8524)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
at android.widget.LinearLayout.forceUniformWidth(LinearLayout.java:594)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:574)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8524)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8524)
at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
確實R.layout.emergency_contacts_list包含R.id.name? – Blackbelt 2012-03-19 19:38:02
是的,它增加了編輯顯示emergency_contacts_list.xml – user1154920 2012-03-19 19:43:28
把你的完整logcat – 2012-03-19 19:44:49