我正在尝试创建一个列表视图,其中TextView可以显示html内容,WebView和其他基本TextView。我试图扩展SimpleAdapter但是我遇到了问题,如果有人能指出错误,我会很高兴我在做。
on onCreate方法
ArrayList<HashMap<String, String>> mylist= resultfromXmlparser();
adap = new MyAdapter(TourLandingPage.this, mylist, R.layout.row,
new String[] {"Name", "desc","Duration","Price","imgurl"}, new int[]
{R.id.productname,R.id.des,R.id.duration,R.id.pricefrom,R.id.photo});
setListAdapter(adap);
My custom Adapter looks like this
private class MyAdapter extends SimpleAdapter {
ArrayList<HashMap<String,String>> elements;
Context ctx;
public MyAdapter(Context context, ArrayList<HashMap<String,String>> mylist,int textViewResourceId,String[] names,int[] resouceid) {
super(context, mylist,textViewResourceId, names,resouceid);
this.elements=mylist;
this.ctx=context;
}
@Override
public int getCount() {
return elements.size();
}
@Override
public Object getItem(int position) {
return elements.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout rowLayout;
if (convertView == null) {
rowLayout = (RelativeLayout)
LayoutInflater.from(ctx).inflate(R.layout.rowfor_tourlist, parent, false);
} else {
rowLayout = (RelativeLayout) convertView;
}
TextView in = (TextView)rowLayout.findViewById(R.id.introduction);
TextView du = (TextView)rowLayout.findViewById(R.id.duration);
TextView pf = (TextView)rowLayout.findViewById(R.id.pricefrom);
TextView pn = (TextView)rowLayout.findViewById(R.id.productname);
WebView wv=(WebView)rowLayout.findViewById(R.id.photo);
in.setText(Html.fromHtml(mylist.get(position).get("desc")));
du.setText(mylist.get(position).get("Duration"));
pf.setText(mylist.get(position).get("Price"));
pn.setText(mylist.get(position).get("Name"));
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(mylist.get(position).get("imgurl"));
return convertView;
}
}//class
我的row.xml文件看起来像
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
TextView
android:id="@+id/productname"
......
LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dip"
android:layout_margin="5dip"
android:layout_below="@id/productname"
android:orientation="horizontal"
android:id="@+id/lay1"
WebView
android:id="@+id/photo"
....
TextView
android:id="@+id/introduction"
......
LinearLayout
TextView
android:id="@+id/duration"
....
TextView
android:id="@+id/pricefrom"
.....
RelativeLayout
我得到的错误是
04-28 19:46:17.749: ERROR/AndroidRuntime(976): Uncaught handler: thread main exiting due to uncaught exception
04-28 19:46:17.769: ERROR/AndroidRuntime(976): java.lang.NullPointerException
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.ListView.setupChild(ListView.java:1693)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.ListView.makeAndAddView(ListView.java:1671)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.ListView.fillDown(ListView.java:637)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.ListView.fillFromTop(ListView.java:694)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.ListView.layoutChildren(ListView.java:1521)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.onLayout(LinearLayout.java:920)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.View.layout(View.java:6831)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.os.Looper.loop(Looper.java:123)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at android.app.ActivityThread.main(ActivityThread.java:4338)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at java.lang.reflect.Method.invoke(Method.java:521)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 19:46:17.769: ERROR/AndroidRuntime(976): at dalvik.system.NativeStart.main(Native Method)
04-28 19:46:17.789: INFO/Process(52): Sending signal. PID: 976 SIG: 3
04-28 19:46:17.799: INFO/dalvikvm(976): threadid=7: reacting to signal 3
04-28 19:46:17.829: INFO/dalvikvm(976): Wrote stack trace to '/data/anr/traces.txt'
答案 0 :(得分:3)
正如我在上面的代码中看到的那样,你的问题出现在“return convertView”中。线。你刚刚返回了一个输入视图,而不是那个膨胀的输入视图。
答案 1 :(得分:1)
不确定这是否是原因,但SimpleAdapter构造函数的最后一个参数都应该是TextViews。