片段上的java.lang.NullPointerException ListViewAdapter.getView(ListViewAdapter.java:60)

时间:2014-04-25 02:52:39

标签: java android android-fragments

1.此ListView适配器集成在片段

public class ListViewAdapter extends BaseAdapter {

  // Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(NewsFragment newsFragment,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}



@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
//protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Declare Variables
    TextView  productname;
    TextView productid;
    TextView producttype;
    ImageView image;

这里是问题inflater =(LayoutInflater)convertView.getContext()       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           // inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 enter code here

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);
    // Get the position
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    productname = (TextView) itemView.findViewById(R.id.product);
    productid = (TextView) itemView.findViewById(R.id.dateOfInput);
    producttype = (TextView) itemView.findViewById(R.id.productprice);

    // Locate the ImageView in listview_item.xml
    image = (ImageView) itemView.findViewById(R.id.flag);

    // Capture position and set results to the TextViews
    productname.setText(resultp.get(NewsFragment.PRODUCTNAME));
    productid.setText(resultp.get(NewsFragment.PRODUCTPRICE));
    producttype.setText(resultp.get(NewsFragment.DATEOFINPUT));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(resultp.get(NewsFragment.IMAGE), image);
    // Capture ListView item click
    itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            resultp = data.get(position);
            Intent intent = new Intent(context, SingleItemView.class);
            // Pass all data rank
            intent.putExtra("productname",resultp.get(NewsFragment.PRODUCTNAME));
            // Pass all data country
    intent.putExtra("productid", resultp.get(NewsFragment.PRODUCTPRICE));
            // Pass all data population
              intent.putExtra("producttype", resultp.get(NewsFragment.DATEOFINPUT));                
            // Pass all data flag
            intent.putExtra("image", resultp.get(NewsFragment.IMAGE));
            // Start SingleItemView Class
            context.startActivity(intent);

        }
    });
    return itemView;
}

}

1 个答案:

答案 0 :(得分:1)

您在初始化LayoutInflatrot

时犯了错误

你已经完成了这个

inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这是错误的,因为convertView在开头会为null。

而你需要做类似

的事情
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

如果你想使用上下文,你需要在Adapter的构造函数中正确初始化它,它当前指向它自己。