问题描述
- Could not execute method of the activity
-
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什么关系
解决方案二:
Imagebutton没有实例化吧,而且没有设置点击事件吧,不然肯定会报空指针异常咯
解决方案三:
你看日志,是那句话不错,调试你总该会吧,断点到那,就啥都明白了
时间: 2024-11-08 22:26:28