尝试将数据从ListView传递到另一个类

时间:2013-06-21 22:51:22

标签: java android android-listview

所以我被困在这几个小时。我正在尝试设置一个异步任务,它会在另一个类中为我的listview加载图像,并且我传递的信息会给我一个错误

任何帮助将不胜感激!

公共类TheResults扩展了Activity {

public static final String DEFAULTNAME = "DefaultFile";
private GETTHEIMAGE imgFetch;
private ArrayList<Item_Info> Info = new ArrayList<Item_Info>();

String Data = null;
String THESTRING = null;
TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.theresults);

    SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
    Data = Search.getString("THERESULTS", null);
    GetINFO();
    populatelist();
}

private void GetINFO() {
    }
    }
}

private void populatelist() {
    ArrayAdapter<Item_Info> adapter = new TheListAdapter();
    ListView list = (ListView) findViewById(R.id.listings);
    list.findFocus();
    list.getWindowToken();
    list.setAdapter(adapter);
}

public class TheListAdapter extends ArrayAdapter<Item_Info> {
    public TheListAdapter() {
        super(TheResults.this, R.layout.items, Info);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item_View = convertView;
        ImageHolder holder = null;

        if (item_View == null) {
            item_View = getLayoutInflater().inflate(R.layout.items, parent, false);
            Item_Info CurrentItem = Info.get(position);
            holder = new ImageHolder();
            holder.ICON_IMG = (ImageView)item_View.findViewById(R.id.icon_image);
            holder.ICON_IMG.setTag(CurrentItem.getItemPIC());
            Drawable MYIMG = imgFetch.GetTheImagedata(this, holder.ICON_IMG);

            holder.ICON_TITLE = (TextView)item_View.findViewById(R.id.icon_title);
            holder.ICON_TITLE.setText(CurrentItem.getItemTITLE());

            holder.ICON_LOCATION = (TextView)item_View.findViewById(R.id.icon_location);
            holder.ICON_LOCATION.setText(CurrentItem.getItemLOCATION());

            holder.ICON_PRICE = (TextView)item_View.findViewById(R.id.icon_price);
            if (CurrentItem.getItemPRICE() == null) {
                holder.ICON_PRICE.setText(CurrentItem.getItemPRICE());
                holder.ICON_PRICE.setBackgroundResource(R.drawable.tempbg);
            } else {
                holder.ICON_PRICE.setBackgroundResource(R.drawable.pricebg);
                holder.ICON_PRICE.setText(CurrentItem.getItemPRICE());
            }

            holder.ICON_DATE = (TextView)item_View.findViewById(R.id.icon_date);
            holder.ICON_DATE.setText(CurrentItem.getItemLOCATION());
        }else{
            holder = (ImageHolder)item_View.getTag();
        }
        return item_View;
    }
}
static class ImageHolder{
    ImageView ICON_IMG;
    TextView ICON_TITLE;
    TextView ICON_LOCATION;
    TextView ICON_PRICE;
    TextView ICON_DATE;
}

}

1 个答案:

答案 0 :(得分:0)

通过查看行后的logcat

06-21 19:15:06.887: E/AndroidRuntime(11408): FATAL EXCEPTION: main

是例外

java.lang.NullPointerException

告诉我们某些东西是null并且抛出异常。如果我们查找引用您项目的代码的第一行,我们将看到问题发生的位置。这里恰好是logcat的下一行

at com.clistc.TheResults$TheListAdapter.getView(TheResults.java:178)

这告诉我们该行是TheResults.java的178并且它在getView()方法中。我们已经确定它就是这条线

Drawable MYIMG = imgFetch.GetTheImagedata(this, holder.ICON_IMG); 

这意味着imgFetch可能是null,并且您尝试在其上调用函数,该函数返回null,因为变量为null。我不知道该变量是什么,但它没有在此行之前初始化

修改

imgFetch未在任何地方初始化。它在这里宣布

private GETTHEIMAGE imgFetch;

但你永远不会通过给它一个值来初始化它。你需要做一些像

这样的事情
private GETTHEIMAGE imgFetch = new GETTHEIMAGE();

假设它有一个空的构造函数