Getting errors when I am trying to run the hellogridview

I am getting some errors when I am trying to run the hellogridview from the Android website.

GridView.java

package com.GridView;

import android.app.Activity; import android.os.Bundle;

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();
            }
        });
    }
}

error with this statement
(GridView) findViewById(R.id.gridview) error with
setAdapter()
error with
OnItemClickListener()
error with
AdapterView<?>
error with
HelloGridView error with
});

all this was copied from the android website.

Image Adapter.java

package com.GridView;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
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 getView1(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 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
    };

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        return null;
    }
}

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:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

i noticed you have not imported the Android GridView class and the AdapterView class. Add the following imports to your GridView.java:

import android.widget.GridView;
import android.widget.AdapterView;

Your compiler is getting very confused. You have named your class GridView, but you should naming it HelloGridView according to the example. GridView is a class beloning to the Android API. I recommend deleting the project and starting again following the instructions exactly, since otherwise you will need to change a few other things manually to make the app work again. Since their example also misses out all of the required import lines, you must add these yourself. You can do this by pressing ctrl+shift+o in Eclipse.

链接地址: http://www.djcxy.com/p/24188.html

上一篇: 滚动时Android背景ListView变黑

下一篇: 当我尝试运行hellogridview时出错