无法使用自定义适配器理解 NullPointerException

发布于 2024-08-30 10:05:34 字数 7647 浏览 7 评论 0原文

我正在尝试创建一个列表视图,它作为可以显示 html 内容的 TextView、WebView 和其他基本 TextView。我尝试扩展 SimpleAdapter 但我遇到了这个问题,如果有人能指出我正在做的错误,我会很高兴。

在 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'

I am trying to create a list view which as TextView that can display html content , an WebView and other basic TextViews.I tried to extend SimpleAdapter but i struck with the problem ,I will be glad if someone can point out the mistake i am doing.

In onCreate method

 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

and my row.xml file looks like

  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

The error i was getting was

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-09-06 10:05:34

正如我在上面的代码中看到的,您的问题出在“return ConvertView;”中。线。您刚刚返回了一个输入视图,而不是膨胀的视图。

As I can see in the code above, your problem was in the "return convertView;" line. You've just returned an input view, not the inflated one.

維他命╮ 2024-09-06 10:05:34

不确定这是否是原因,但 SimpleAdapter 构造函数的最后一个参数应该都是 TextViews。

SimpleAdapter

Not sure if this is the cause but the last parameter to the SimpleAdapter constructor should all be TextViews.

SimpleAdapter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文