一个GridView,点击能够互换,执行报LinearLayout cannot be cast to android.widget.GridView这个错误

privateRandomrandom;privateGridViewgridView;privateList<Map<String,Object>>dataList;p... private Random random;
private GridView gridView;
private List<Map<String,Object>>dataList;
private int[] icon={R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,R.drawable.img05,R.drawable.img06};
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.gridView);
gridView.setOnItemClickListener(this);
dataList=new ArrayList<Map<String,Object>>();

getData();
adapter=new SimpleAdapter(MainActivity.this, getData(), R.layout.item, new String[]{"image","text"}, new int[]{R.id.image});
gridView.setAdapter(adapter);
}
private List<Map<String,Object>> getData() {
for(int i=1;i<=50;i++)
{
Map<String,Object>map=new HashMap<String, Object>();
random=new Random();
int a=random.nextInt(6);
map.put("image", icon[a]);
//map.put("text", iconName[i]);
dataList.add(map);
}
return dataList;
}
private ImageView imageFirst;
private ImageView imageSecond;
private int firstX;
private int firstY;
private int secondX;
private int secondY;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(imageFirst==arg1)
{

imageFirst.setColorFilter(null);
imageFirst=null;
return;
}
if (imageFirst == null) {
imageFirst = (ImageView) arg1;
firstX = (int) arg1.getX();
firstY = (int) arg1.getY();
imageFirst.setColorFilter(Color.parseColor("#55ff0000"));
} else {
imageSecond = (ImageView) arg1;
secondX = (int) arg0.getX();
secondY = (int) arg0.getY();
exchange(imageFirst,imageSecond);
imageFirst.setColorFilter(null);
imageFirst=null;
imageSecond=null;
}

}
private void exchange(ImageView image1,ImageView image2)
{
image1.setX(secondX);
image1.setY(secondY);
image2.setX(firstX);
image2.setY(firstY);

return;
}
展开
 我来答
爱说车的小哥
2015-11-17 · 超过62用户采纳过TA的回答
知道小有建树答主
回答量:280
采纳率:50%
帮助的人:81.4万
展开全部
gridView=(GridView) findViewById(R.id.gridView);
你这个R.id.gridView是GridView吗
追问
是的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式