Cannot understand NullPointerException with custom adapter

Posted by ganesh on Stack Overflow See other posts from Stack Overflow or by ganesh
Published on 2010-04-28T14:37:58Z Indexed on 2010/04/28 14:53 UTC
Read the original article Hit count: 303

Filed under:
|
|

hi, 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> 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'

© Stack Overflow or respective owner

Related posts about android

Related posts about custom