当我尝试从Android网站运行hellogridview时,我遇到了一些错误。
GridView.java
包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();
}
});
}
}
本声明的错误
(GridView) findViewById(R.id.gridview)
与。的错误
setAdapter()
与。的错误
OnItemClickListener()
与。的错误
AdapterView<?>
与。的错误
HelloGridView
与。的错误
});
所有这些都是从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" />
答案 0 :(得分:3)
我注意到你没有导入Android GridView类和AdapterView类。将以下导入添加到GridView.java:
import android.widget.GridView;
import android.widget.AdapterView;
答案 1 :(得分:2)
您的编译器变得非常困惑。您已将类命名为GridView,但您应根据示例将其命名为HelloGridView。 GridView是一个属于Android API的类。我建议删除项目并按照说明完全重新开始,否则您需要手动更改其他一些内容以使应用程序再次运行。由于他们的示例也错过了所有必需的导入行,因此您必须自己添加这些行。您可以通过在Eclipse中按ctrl + shift + o来完成此操作。