切换按钮的网格视图

时间:2014-08-23 19:29:21

标签: android gridview togglebutton

我得到空指针异常

以下是使用的GridAdaper.java的代码:

 package com.example.toggle;

 import java.util.ArrayList;
 import java.util.List;

 import com.example.toggle.R;

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 import android.widget.Toast;
import android.widget.ToggleButton;

public class GridAdapter extends ArrayAdapter{
private Context context;
private int layoutResourceId;
private ArrayList<String> data = new ArrayList();


public GridAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context= context;
    this.layoutResourceId = textViewResourceId;
    this.data = objects;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View gridItem = convertView;
    ViewHolder holder = null;
    if(gridItem == null)
    {
        LayoutInflater inflater = (LayoutInflater)        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gridItem = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.toggle = (ToggleButton) gridItem.findViewById(R.id.togglebutton);
        gridItem.setTag(holder);
    }
    else
        holder = (ViewHolder) gridItem.getTag();
    String item = (String) data.get(position);
    data.add("add");
    if(item == "add")
        holder.toggle.setBackgroundResource(R.drawable.add_grid);

    return gridItem;
     }


   }

     class ViewHolder
   {
       ToggleButton toggle;
    }

MainActivity.java的代码是:

 public class MainActivity extends Activity {
    ArrayList<String> al;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView grid = (GridView) findViewById(R.id.gridView);
    al.add("add");
    GridAdapter ada = new GridAdapter(MainActivity.this, R.layout.grid_item, al);
    grid.setAdapter(ada);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

main_activity.xml是:

<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>

并且griditem.xml是:

<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/togglebutton"
android:layout_width="match_parent"
android:layout_height="match_parent" 
>


 </ToggleButton>

add_grid.xml工作正常我已经检查过了! 请帮帮我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

在onCreate方法中初始化arrayList:

arrayList = new ArrayList<String>();