XML中有3個按鈕,實現後出現空指針,代碼如下
item.XML
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/on"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/off"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/voice" />
</LinearLayout>
</LinearLayout>
main.java
public class MainActivity extends FragmentActivity {
public static Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
GridView list = (GridView) findViewById(R.id.list);
MyAdapter gAdapter = new MyAdapter(dev_list, this);
list.setAdapter(gAdapter);
final Integer[] images = new Integer[]{R.id.on, R.id.off, R.id.voice};
dev_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (images[position]) {
case R.id.on:
Toast.makeText(getApplicationContext(), "說話",
Toast.LENGTH_SHORT).show();
break;
case R.id.off:
Toast.makeText(getApplicationContext(), "停止",
Toast.LENGTH_SHORT).show();
break;
case R.id.voice:
Toast.makeText(getApplicationContext(), "聽話",
Toast.LENGTH_SHORT).show();
break;
}
}
});
}
求指點,盡量詳細的,腦子不太夠用
關系有點亂
GridView list = (GridView) findViewById(R.id.list); //這是gridview
MyAdapter gAdapter = new MyAdapter(dev_list, this); //adapter
list.setAdapter(gAdapter); //gridview和adapter
final Integer[] images = new Integer[]{R.id.on, R.id.off, R.id.voice};
dev_list.setOnItemClickListener //這裡怎麼監聽dev_list?這個adapter和gridview什麼關系