android gridview 空指针

vh.imageViewvh.setImageResource(getItem(i).dataicon);//这两句提示有空指针
vh.textViewvh.setText(getItem(i).dataName);
package com.zhangyi.zyapp26_27;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

/**

  • Created by me on 2016-11-3. */

public class BViewSwitcher extends Activity {

public static final int NUMBER_PER_SCREEN = 12;
public static class DataItem{
    public String dataName;
    public int dataicon;
}
static class ViewHolder{
    ImageView imageViewvh;
    TextView textViewvh;
}

private ArrayList<DataItem> items = new ArrayList<>();
private int screenNo = 1;
private int screenCount;
ViewSwitcher myviewswitcher;
LayoutInflater inflater;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b_viewswitcher);

    inflater = LayoutInflater.from(BViewSwitcher.this);
    for (int i = 0; i < 40; i++) {
        String dataname ="";
        int drawable =0;
        DataItem item=new DataItem();
        switch ((int) (Math.random() * 3)) {
            case 0:dataname="Chrome "+i;drawable = R.mipmap.ic_launcher2;break;
            case 1:dataname="Picture "+i;drawable = R.mipmap.ic_launcher3;break;
            case 2:dataname="Shop "+i;drawable = R.mipmap.ic_launcher4;break;
        }
        item.dataName = dataname;
        item.dataicon = drawable;
        items.add(item);
    }
    screenCount = items.size() % NUMBER_PER_SCREEN == 0 ?
            items.size() / NUMBER_PER_SCREEN : items.size() / NUMBER_PER_SCREEN + 1;
    myviewswitcher = (ViewSwitcher) findViewById(R.id.myviewswitcher);
    myviewswitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return inflater.inflate(R.layout.datagridview, null);
        }
    });
    next(null);
}

public void previous(View view) {
    if (screenNo > 1) {
        screenNo--;
        myviewswitcher.setInAnimation(this, R.anim.push_left_in);
        ((GridView) myviewswitcher.getNextView()).setAdapter(adapter);
        myviewswitcher.showPrevious();
    }
}

public void next(View view) {
    if (screenNo < screenCount) {
        screenNo++;
        myviewswitcher.setInAnimation(this, R.anim.push_right_in);
        ((GridView) myviewswitcher.getNextView()).setAdapter(adapter);
        myviewswitcher.showNext();
    }
}
private BaseAdapter adapter = new BaseAdapter() {
    @Override
    public int getCount() {
        if (screenNo == screenCount && items.size() % NUMBER_PER_SCREEN != 0) {
            return items.size() % NUMBER_PER_SCREEN;
        }
        return NUMBER_PER_SCREEN;
    }

    @Override
    public DataItem getItem(int i) {
        return items.get((screenNo - 1) * NUMBER_PER_SCREEN + i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view1 = view;
        ViewHolder vh=new ViewHolder();
        ViewGroup viewGroup2=(ViewGroup) getLayoutInflater().inflate(R.layout.datagridview, null);
        vh.imageViewvh = (ImageView) viewGroup2.findViewById(R.id.imagedata);
        vh.textViewvh = (TextView) viewGroup2.findViewById(R.id.textdata);
        try {
            vh.imageViewvh.setImageResource(getItem(i).dataicon);//这两句提示有空指针
            vh.textViewvh.setText(getItem(i).dataName);
        } catch (Exception e) {
            e.printStackTrace();
        }

// ViewGroup parent1 = (ViewGroup) vh.imageViewvh.getParent();

// if (parent1 != null) {
// parent1.removeAllViews();
// }
//
// ViewGroup parent11 = (ViewGroup) vh.textViewvh.getParent();
// if (parent11 != null) {
// parent11.removeAllViews();
// }
// viewGroup2.addView(vh.imageViewvh);
// viewGroup2.addView(vh.textViewvh);

// if (view1 == null) {
// view1 = inflater.inflate(R.layout.datagridview, viewGroup,false);
// }
// ImageView imageView = (ImageView) viewGroup2.findViewById(R.id.imagedata);
// TextView textView = (TextView) viewGroup2.findViewById(R.id.textdata);
// try {
//
// imageView.setImageResource(getItem(i).dataicon);
// imageView.setImageResource(R.mipmap.ic_launcher2);
// textView.setText(getItem(i).dataName);
// }catch (Exception e){
// e.printStackTrace();
// }
return viewGroup2;
}
};
}

2个回答

你能把logcat的红字也贴上来吗?

你的ViewHolder里边的数据成员并没有实例化,当然会报错了。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!