无法理解的NullPointerException与自定义适配器 [英] Cannot understand NullPointerException with custom adapter

查看:94
本文介绍了无法理解的NullPointerException与自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个列表视图,因为TextView中,可以显示HTML内容,一个的WebView等基本TextViews.I试图扩大SimpleAdapter但我打的问题,我会很高兴,如果有人能指出错误我所做的一切。

在onCreate方法

 的ArrayList< HashMap的<字符串,字符串>> mylist中= resultfromXmlparser();

 ADAP =新MyAdapter(TourLandingPage.this,mylist中,R.layout.row,
            新的String [] {姓名,说明,持续时间,价格,imgUrl的},新的INT []
            {R.id.productname,R.id.des,R.id.duration,R.id.pricefrom,R.id.photo});

setListAdapter(ADAP);


我的自定义适配器看起来是这样的

 私有类MyAdapter扩展SimpleAdapter {

             ArrayList的< HashMap的<字符串,字符串>>元素;
             上下文CTX;
             公共MyAdapter(上下文的背景下,ArrayList的< HashMap的<字符串,字符串>> mylist中,INT textViewResourceId,字符串[]名,INT [] resouceid){
             超(背景下,mylist中,textViewResourceId,名称,resouceid);
             this.elements = mylist中;
             this.ctx =背景;

             }
             @覆盖
             公众诠释getCount将(){
                  返回elements.size();
                 }
             @覆盖
             公共对象的getItem(INT位置){
                  返回elements.get(位置);
              }
            @覆盖
            众长getItemId(INT位置){
                  返回的位置;
                 }


                  @覆盖
           公共查看getView(INT位置,查看convertView,ViewGroup中父){
             RelativeLayout的RowLayout的;
                    如果(convertView == NULL){
                     RowLayout的=(RelativeLayout的)
                         LayoutInflater.from(CTX).inflate(R.layout.rowfor_tourlist,父母,假);

               } 其他 {

                RowLayout的=(RelativeLayout的)convertView;
                     }


                  TextView的时间=(TextView中)rowLayout.findViewById(R.id.introduction);
                  TextView的杜=(TextView中)rowLayout.findViewById(R.id.duration);
                  TextView的PF =(TextView中)rowLayout.findViewById(R.id.pricefrom);
                  TextView的PN =(TextView中)rowLayout.findViewById(R.id.productname);
                  的WebView WV =(web视图)rowLayout.findViewById(R.id.photo);
                  in.setText(Html.fromHtml(mylist.get(位置)获得(降序)));
                  du.setText(mylist.get(位置)获得(持续时间));
                  pf.setText(mylist.get(位置)获得(价格));
                  pn.setText(mylist.get(位置)获得(姓名));
                  wv.getSettings()setJavaScriptEnabled(真)。
                  wv.loadUrl(mylist.get(位置)获得(imgUrl的));


                 返回convertView;
             }
         }//类
 

和我的row.xml文件看起来像

  RelativeLayout的
     的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =FILL_PARENT

  的TextView
       机器人:ID =@ + ID /产品名称
       ......

  的LinearLayout
         机器人:layout_width =WRAP_CONTENT
         机器人:layout_height =60dip
         机器人:layout_margin =5dip
         机器人:layout_below =@用户名/产品名
         机器人:方向=横向
         机器人:ID =@ + ID / lay1

         的WebView
          机器人:ID =@ + ID /摄
          ....
         的TextView
           机器人:ID =@ + ID /简介
           ......
   的LinearLayout

   的TextView
      机器人:ID =@ + ID /时间
      ....
   的TextView
     机器人:ID =@ + ID / pricefrom
      .....

RelativeLayout的
 

我得到的错误是

  04-28 19:46:17.749:ERROR / AndroidRuntime(976):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
04-28 19:46:17.769:ERROR / AndroidRuntime(976):显示java.lang.NullPointerException
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.ListView.setupChild(ListView.java:1693)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.ListView.makeAndAddView(ListView.java:1671)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.ListView.fillDown(ListView.java:637)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.ListView.fillFromTop(ListView.java:694)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.ListView.layoutChildren(ListView.java:1521)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.AbsListView.onLayout(AbsListView.java:1113)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1108)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.onLayout(LinearLayout.java:920)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.LinearLayout.onLayout(LinearLayout.java:918)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.View.layout(View.java:6831)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.ViewRoot.performTraversals(ViewRoot.java:996)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.os.Handler.dispatchMessage(Handler.java:99)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.os.Looper.loop(Looper.java:123)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在android.app.ActivityThread.main(ActivityThread.java:4338)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在java.lang.reflect.Method.invokeNative(本机方法)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在java.lang.reflect.Method.invoke(Method.java:521)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-28 19:46:17.769:ERROR / AndroidRuntime(976):在dalvik.system.NativeStart.main(本机方法)
04-28 19:46:17.789:信息/工艺(52):发送信号。 PID:976 SIG:3
04-28 19:46:17.799:信息/ dalvikvm(976):主题ID = 7:反应信号3
04-28 19:46:17.829:信息/ dalvikvm(976):写堆栈跟踪/data/anr/traces.txt
 

解决方案

不知道这是原因,但最后一个参数SimpleAdapter构造都应该TextViews。

<一个href="http://developer.android.com/reference/android/widget/SimpleAdapter.html#SimpleAdapter%28android.content.Context,%20java.util.List%3C?%20extends%20java.util.Map%3Cjava.lang.String,%20?%3E%3E,%20int,%20java.lang.String%5b%5d,%20int%5b%5d%29"相对=nofollow> SimpleAdapter

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'

解决方案

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

SimpleAdapter

这篇关于无法理解的NullPointerException与自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆