我有一个网格布局,屏幕顶部有一个 EditText 字段。我正在实现基本适配器来填充网格布局的值。
我正在使用的代码如下。我的主要活动
public class MainActivityDynamic extends Activity{
GridView gridView;
GridObject[] planets;
ArrayList<GridObject> myObjects;
Button Go;
ImageAdapter adapter;
int position;
boolean[] itemChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gridlayout);
Go = (Button)findViewById(R.id.bGO);
gridView = (GridView) findViewById(R.id.gV);
String[] primarycontext = {"first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","leventh","twelth","thirteenth","fouteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twenteth","twentyfirst","twentysecond", "twentythird","twentythird","twentyfouth"};
gridView.setAdapter(new ImageAdapter(this, primarycontext));
gridView.setOnItemClickListener(new GridView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
// TODO Auto-generated method stub
//myObjects.get(position).setState(1);
//myAdapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), "clicked on "+position, Toast.LENGTH_SHORT).show();
}
});
Go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//adapter.notifyDataSetChanged();
int len = 10;
int cnt = 0;
String selectImages = "";
for (int i = 0;i<len; i++){
if (adapter.itemChecked[i]){
cnt++;
}
}
if(cnt == 0){
Toast.makeText(getBaseContext(), "please select atleast one", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getBaseContext(), "you have selected" + cnt + "boxes", Toast.LENGTH_SHORT).show();
}
String item = (String) adapter.getItem(position);
Toast.makeText(getBaseContext(), "selected fields"+item, Toast.LENGTH_SHORT).show();
}
});
}
}
public class ImageAdapter extends BaseAdapter
{
Context mContext;
public ImageAdapter(Context c,String[] primarycontext)
{
mContext = c;
this.primarycontext = primarycontext;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return providers.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return primarycontext[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = new View(mContext);
v = li.inflate(R.layout.icon, null);
}
else
{
v = convertView;
}
ImageView iv = (ImageView)v.findViewById(R.id.grid_item_image);
iv.setImageResource(R.drawable.folder_0);
CheckBox CBx = (CheckBox)v.findViewById(R.id.cBIon);
CBx.setText(primarycontext[position]);
CBx.setChecked(isItemChecked(position));
return v;
}
private boolean isItemChecked(int position) {
// TODO Auto-generated method stub
System.out.println("the returning items="+itemChecked);
return itemChecked;
}
}
}
在主布局中,我有一个网格视图,上面有一个带有文本和图像的复选框。我有一个 Submit(Go) 按钮,单击该按钮应捕获在主要活动中选中的复选框的所有名称。我怎样才能做到这一点?当我运行此代码并单击 Go 时,应用程序崩溃并返回空指针异常。
错误
11-25 05:43:50.390: E/AndroidRuntime(519): FATAL EXCEPTION: main
11-25 05:43:50.390: E/AndroidRuntime(519): java.lang.NullPointerException
11-25 05:43:50.390: E/AndroidRuntime(519): at com.example.kam1.MainActivityDynamic$2.onClick(MainActivityDynamic.java:73)
11-25 05:43:50.390: E/AndroidRuntime(519): at android.view.View.performClick(View.java:2408)
11-25 05:43:50.390: E/AndroidRuntime(519): at android.view.View$PerformClick.run(View.java:8816)
11-25 05:43:50.390: E/AndroidRuntime(519): at android.os.Handler.handleCallback(Handler.java:587)