自定义arrayadapter内的Android gridview getchildat()空指针异常

时间:2014-03-18 15:20:35

标签: java android gridview

我不确定如何解决这个问题。我试图在gridview中找到项目,但我在尝试获取项目时得到一个空指针?我正在执行此操作的函数是resetbutton()和似乎是null的代码行是gridViewlayout.getChildAt(goodguyposition).setBackgroundResource(R.drawable.goodguybutton);我不认为我正在解析任何事情,所以我可以理解null,但我可以'抓住gridview id,我不知道为什么? gridViewlayout = (GridView) findViewById(R.id.gridView1);我是否需要从主活动或getView区域传递它?

activity_main.xml布局

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="11"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</GridView>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="O"
android:id="@+id/grid_item_label"
android:layout_column="1" 
android:background = "@drawable/roundedbutton"/>

customgridviewadapter.java

public class CustomGridViewAdapter extends ArrayAdapter<Item> {

    private static final String TAG = "CustomGridViewAdapter";

    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();
    GridView gridViewlayout;
    View gridView;

    public CustomGridViewAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        Log.d(TAG, "Position before convertView " + position );


        if (convertView == null) 
            {


                gridView = new View(context);

                // get layout 
                gridView = inflater.inflate(R.layout.button_item, null);


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        Log.d(TAG, "Position before convertView " + position );


        if (convertView == null) 
            {


                gridView = new View(context);

                // get layout 
                gridView = inflater.inflate(R.layout.button_item, null);

                //gridView.setMinimumHeight(ButtonGridView.height/6);

               // Log.d(TAG, "convertView height is " + ButtonGridView.height/6);

                Button btn = (Button) gridView.findViewById(R.id.grid_item_label);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {

                        Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();

                        if (view instanceof Button) {
                            Button btn = (Button) view;


                            if (data.get(position).getTitle().equals("NormalGood"))
                                {
                                      resetbutton();




                        }
                        //context.itemClicked(position);
                    }
                });

    } 
        else 
            {
                gridView = (View) convertView;
            }




        return gridView;


    }


 void resetbutton()
    {
        int goodguyposition = getGoodGuy();

        gridViewlayout = (GridView) findViewById(R.id.gridView1);

        gridViewlayout.getChildAt(goodguyposition).setBackgroundResource(R.drawable.goodguybutton);

}

}

1 个答案:

答案 0 :(得分:0)

您的数据中包含x项的事实并不意味着GridView有x个子项,因为适配器重用了视图,所以

gridViewlayout.getChildAt(goodguyposition)

可能是null

如果要在GridView中的项目上设置背景,请在getView方法中执行此操作,如

if (position == goodguyposition){

    convertView.setBackgroundResource(...); 

}