2 wihte hat wihte_hat 于 2016.09.13 09:05 提问

GridView自定义适配器,赋值时报空指针

public class HomeActivity extends Activity{

private GridView gv_home;
private String[] mTitleStr;
private int[] mDrawableIds;

@Override
protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    setContentView(R.layout.activity_home);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        // 激活状态栏
        tintManager.setStatusBarTintEnabled(true);
        //设置系统栏设置颜色
        //tintManager.setTintColor(R.color.red);
        //给状态栏设置颜色
        tintManager.setStatusBarTintResource(R.color.blue);

    }
    initUI();
    //初始化数据
    initData();
}

private void initData() {
    mTitleStr=new String[] {
            "手机防盗","通信卫士","软件管理","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"
    };
    mDrawableIds=new int[] {
            R.drawable.home_safe,R.drawable.home_callmsgsafe,R.drawable.home_apps,
            R.drawable.home_taskmanager,R.drawable.home_netmanager,R.drawable.home_trojan,
            R.drawable.home_sysoptimize,R.drawable.home_tools,R.drawable.home_settings
    };

    gv_home.setAdapter(new MyAdapter());
}

private void initUI() {
    gv_home = (GridView) findViewById(R.id.gv_home);
}

class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return mTitleStr.length;
    }

    @Override
    public Object getItem(int position) {
        return mTitleStr[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=View.inflate(getApplicationContext(), R.layout.gridview_item,null);
        ImageView iv_icon=(ImageView)findViewById(R.id.iv_icon);
        TextView tv_title=(TextView)findViewById(R.id.tv_title);

        tv_title.setText(mTitleStr[position]);//此处报空指针,求助
        iv_icon.setBackgroundResource(mDrawableIds[position]);

        return view;
    }
}

}

3个回答

u011966283
u011966283   2016.09.13 09:35
已采纳

TextView tv_title=(TextView)view.findViewById(R.id.tv_title);
你的TextView,关联的不是gridview_item的,

wihte_hat
wihte_hat 已采纳,谢谢
一年多之前 回复
hushaoxi
hushaoxi 楼主正解,直接使用findViewById的含义是activity查找控件的方法,你需要的是ItemView查找控件
一年多之前 回复
bdmh
bdmh   Ds   Rxr 2016.09.13 09:15

你先检查tv_title是不是null

xuexiaoyang155
xuexiaoyang155   2016.09.13 11:45

TextView tv_title=(TextView)convertView.findViewById(R.id.tv_title);//改这里
return convertView;

    //如果我写,最好添加ViewHolder类
    public class ViewHolder{
            TextView tv_title
    }

    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null){
        holder = new ViewHolder();
                    ......//布局那些自己添上去吧,懒得弄了~~
                    holder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
            holder.tv_title.setText(mTitleStr[position]);
            return convertView;

    }
wihte_hat
wihte_hat 知道了,谢谢
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!