我想讓這個gridview可點擊。一切正常,但最後一個單元格。當我點擊最後一個單元格,即第8個單元格時,程序崩潰。任何有關它崩潰的建議都會有幫助。GridView可點擊
public class MainView extends Activity {
\t
\t ImageView back, home, site;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
\t setContentView(R.layout.mainview);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
\t
\t Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(), TheShow.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(), Exhibitor.class);
}
if(position ==2){
myIntent = new Intent(v.getContext(), Visitor.class);
}
if(position ==3){
myIntent = new Intent(v.getContext(), Conference.class);
}
if(position ==4){
myIntent = new Intent(v.getContext(), ContactUs.class);
}
if(position == 5){
\t myIntent = new Intent(v.getContext(), VisitorReg.class);
}
if(position == 6){
\t myIntent = new Intent(v.getContext(), ExhibitorReg.class);
}
if(position == 7){
\t Toast.makeText(getApplicationContext(), "Pending", 4000).show();
}
startActivity(myIntent); \t
\t
}
});
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("The Show", R.drawable.globecopy1));
items.add(new Item("Exhibitor", R.drawable.workcopy1));
items.add(new Item("Visitor", R.drawable.visitor1copy1));
items.add(new Item("Conference", R.drawable.conferencecopy1));
items.add(new Item("Contact Us", R.drawable.contactus1copy1));
items.add(new Item("Visitor Registration", R.drawable.registercopy1));
items.add(new Item("Exhibitor Registration", R.drawable.exregistercopy1));
items.add(new Item("Delegate Registration", R.drawable.anothercopy1));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backg" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/header" />
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_above="@+id/TableLayout1"
android:layout_gravity="bottom"
android:layout_margin="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="4dp"
android:background="#00FF0000"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<TableLayout
android:id="@+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#505050" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/bottom1" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/bottom2" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:src="@drawable/bottom3" />
</TableRow>
</TableLayout>
</RelativeLayout>
請張貼logcat的 – Andrain
*任何建議,爲什麼它會崩潰很有幫助。* <=沒有合乎邏輯的解釋,編程是一個神奇的...你應該住在這 – Selvin