开发者

Android grid view tutorial problem (error: cannot cast from View to GridView)

开发者 https://www.devze.com 2023-04-05 20:44 出处:网络
I\'ve been trying to follow the android tutorials and stuck with the Grid View Tutorial. I copied the codes for main.xml,the onCreate method, and the ImageAdapter class from this tutorial. However, I

I've been trying to follow the android tutorials and stuck with the Grid View Tutorial.

I copied the codes for main.xml, the onCreate method, and the ImageAdapter class from this tutorial. However, I got several errors, the first being "Cannot cast from View to GridView".

I searched for a solution online and mostly people who 开发者_C百科encountered a similar error resolved it by doing proper imports. I use Eclipse and used its "CTRL+SHIFT+O" for automatic import.

So here is my GridView.java

package com.example.hellogridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class GridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Here is my ImageAdapter.java

package com.example.hellogridview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

Finally, the main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
/>

Can anyone tell me what am I doing wrong? Thanks.


Type conflict.

Your Activity can't have name GridView, because it conflicts with android.widget.GridView. Compiler thinks that you want to cast to GridView extending Activity, not the widget class.

Solutions:

  1. rename you GridView
  2. use fully qualified type here: android.widget.GridView gridview = (android.widget.GridView) findViewById(R.id.gridview);


Since I found a few ways to make this tutorial work (thanks to the answer by mice for that), I wanted to give my own detailed answer in case other people who just started with the Android tutorials encounter the same problem.

In short two main issues were naming the Activity as "HelloGridVIew" and removing "import.android.R;" in both classes. Below is a more detailed description of what I did.

1.

When creating the Android application it is important how to name the activity properly to avoid name conflicts and other issues. Namely, in Eclipse do File->New->Android Project. Then type project name "HelloGridVIew" as suggested in the tutorial. The project name I chose as

com.example.hellogridview

Then Eclipse automatically fills the field Create Activity with "HelloGridVIewActivity". But it is important to change it to "HelloGridVIew" because the code in the tutorial implicitly assumes this activity, i.e. in this line of the onCreate() method

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

2.

Copy-paste the onCreate() method from the tutorial in HelloGridView.java.

3.

Create a new class (File->New->Class) named ImageAdapter and copy-paste the corresponding code from the tutorial.

4.

Press CTRL+SHIFT+O in both HelloGridView.java and ImageAdapter.java. This will make Eclipse to import necessary things. However at this step in both classes Eclipse imports the following

import android.R;

which is not correct. Remove this line from both classes. This step I found on stackoverflow before asking this question.

This made my example work. Hope this might help others as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消