简体   繁体   中英

Getting Null pointer Exception in CustomListViewAdapter in Android

I am inserting my List in a ListView , Which has some constant image and dynamic images with text. So I wrote my CustomListViewAdapter , but when I am calling this I am getting NULL pointer exception at

        holder.desc.setText(EventItems.getDesc());

in CustomListViewAdapterForEvent .

Here is my complete code.

Events.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/drop_shadow"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/imageView2"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="343dp"

        android:cacheColorHint="#515151"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:fadeScrollbars="false"
        android:fastScrollEnabled="true"
        android:scrollX="0px"
        android:scrollY="8px"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbarSize="20dip"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical"
        android:smoothScrollbar="true" >
    </ListView>
</LinearLayout>



<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
     android:scaleType="fitXY"
    android:src="@drawable/cat_header" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView2"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_alignRight="@+id/imageView2"
    android:layout_alignTop="@+id/imageView2" >

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:src="@drawable/menu_icon1" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:src="@drawable/add_friend" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:src="@drawable/message_icon" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:src="@drawable/dollar_3d" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:src="@drawable/friend_icons" />

  </LinearLayout>

     </RelativeLayout>

EventList.xml

           <?xml version="1.0" encoding="utf-8"?>
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     android:background="@android:color/transparent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/eventImage"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:contentDescription="@drawable/ic_launcher3"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/sweetIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:contentDescription="@drawable/ic_launcher3"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:scaleType="fitXY"
            android:src="@drawable/aweet_icon" />

        <TextView
            android:id="@+id/sweetNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="sweetNo" />

        <ImageView
            android:id="@+id/facebookIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher1" />

        <ImageView
            android:id="@+id/googleIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher1" />

        <ImageView
            android:id="@+id/tweeterIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher1" />

        <ImageView
            android:id="@+id/pIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher1" />
    </LinearLayout>

    <TextView
        android:id="@+id/eventDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="38dp"
        android:text="Event Description" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/iAmThereIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="0.25"
            android:scaleType="fitXY"
            android:src="@drawable/i_m_there_icon" />

        <ImageView
            android:id="@+id/maybeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:scaleType="fitXY"
            android:src="@drawable/may_be_icon" />

    </LinearLayout>

       </LinearLayout>



       </RelativeLayout>

CustomListViewAdapterForEvent.java

            public class CustomListViewAdapterForEvent extends ArrayAdapter<EventItems>     {

     Context context;

         public CustomListViewAdapterForEvent(Context context, int resourceId,
        List<EventItems> items) {
    super(context, resourceId, items);
    this.context = context;
 }

 /*private view holder class*/
 private class ViewHolder {
   ImageView eventImage;
   TextView sweetNo;
   TextView desc;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder = null;
   Log.e("TAG ", "1111111111111111");
   EventItems EventItems = getItem(position);
   Log.e("TAG ", "2222222222222222222");
   LayoutInflater mInflater = (LayoutInflater) context
           .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
   Log.e("TAG ", "3333333333333333333");
   if (convertView == null) {
       Log.e("TAG ", "444444444444444444444");
       convertView = mInflater.inflate(R.layout.event_list, null);
       holder = new ViewHolder();
       holder.desc = (TextView) convertView.findViewById(R.id.desc);
       holder.sweetNo = (TextView) convertView.findViewById(R.id.sweetNo);
       holder.eventImage = (ImageView) convertView.findViewById(R.id.eventImage);
       Log.e("TAG ", "5555555555555555");
       convertView.setTag(holder);
   } else{

       Log.e("TAG ", "6666666666666666");
       holder = (ViewHolder) convertView.getTag();


   }

   Log.e("TAG ", "77777777777777777777777");
   holder.desc.setText(EventItems.getDesc());
   Log.e("TAG ", "88888888888888888");
   holder.sweetNo.setText(EventItems.getSweetNo());
   holder.eventImage.setImageBitmap(EventItems.getEventImage());

   return convertView;
  }
     }

EventItems.java

  import android.graphics.Bitmap;

       public class EventItems {
private Bitmap eventImage;
  private String sweetNo;
 private String desc;



 public EventItems(Bitmap eventImage, String sweetNo, String desc) {

this.eventImage = eventImage;
this.sweetNo = sweetNo;
this.desc = desc;
     }

   public Bitmap getEventImage() {
return eventImage;
   }
      public void setEventImage(Bitmap eventImage) {
this.eventImage = eventImage;
   }
      public String getSweetNo() {
return sweetNo;
   }
 public void setSweetNo(String sweetNo) {
this.sweetNo = sweetNo;
  }
    public String getDesc() {
return desc;
      }
     public void setDesc(String desc) {
this.desc = desc;
    }

    }

Events.java

    public class Events extends Activity {
ListView listView;
List<EventItems> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.events); 

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new            StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    final  AlertDialog ad=new AlertDialog.Builder(this).create();
    rowItems = new ArrayList<EventItems>();

     Bitmap bm =getImageBitmap("http://192.168.1.5/Upload/Thumbnail/1/iqygbbfn.jpg");
      EventItems item = new EventItems(bm,"hi", "ji");
      rowItems.add(item); 

     listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapterForEvent adapter = new CustomListViewAdapterForEvent(this,
             R.layout.event_list, rowItems);
     listView.setAdapter(adapter);

}



//Function for getting image from URL
   private Bitmap getImageBitmap(String url) 
     {
    Bitmap bm = null;
    try 
    {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
   } catch (IOException e) 
   {
       Log.e("TAG", "Error getting bitmap", e);
   }
   return bm;
  } 

}

Take a look on your EventList.xml , you have no element with id desc there, that is why you get null here (but you have eventDesc instead):

holder.desc = (TextView) convertView.findViewById(R.id.desc);

I believe it's just a typo and the correct line should be as following:

holder.desc = (TextView) convertView.findViewById(R.id.eventDesc);

PS Please read how to use debugger, it will make your life easier - placing a lot of Log objects is not the best solution for finding error.

Change this

holder.desc = (TextView) convertView.findViewById(R.id.desc);

To

holder.desc = (TextView) convertView.findViewById(R.id.eventDesc);

in your getView(....) into CustomListViewAdapterForEvent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM