当我尝试运行hellogridview时出错
当我尝试从Android网站运行hellogridview时遇到一些错误。
GridView.java
包com.GridView;
导入android.app.Activity; 导入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();
}
});
}
}
与此声明错误
(GridView) findViewById(R.id.gridview)
错误
setAdapter()
错误
OnItemClickListener()
错误
AdapterView<?>
错误
HelloGridView
错误
});
所有这些都是从android网站复制的。
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" />
我注意到你没有导入Android GridView类和AdapterView类。 将以下导入添加到GridView.java中:
import android.widget.GridView;
import android.widget.AdapterView;
你的编译器变得非常困惑。 你已经命名了你的类GridView,但是你应该根据例子命名它为HelloGridView。 GridView是Android API的一个类。 我建议删除该项目并按照说明重新开始,否则您需要手动更改其他一些内容才能使应用程序再次运行。 由于他们的例子也错过了所有必需的导入行,您必须自己添加这些行。 您可以通过在Eclipse中按Ctrl + Shift + o来完成此操作。
链接地址: http://www.djcxy.com/p/24187.html上一篇: Getting errors when I am trying to run the hellogridview
下一篇: scrollingCache?