今天遇到这个问题,参考了博文 http://blog.youkuaiyun.com/ezhong0812/article/details/6277814
问题:
layout文件定义了2个button
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="One" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:text="Two" />
java文件中做了不同的onClick动作,点击后触发2个不同的Activity
Button button_send = (Button)findViewById(R.id.button_send);
button_send.setOnClickListener(new tryClickListener());
Button button2 = (Button)findViewById(R.id.button_two);
button2.setOnClickListener(new OnClickListener(){});
运行的时候提示:应用程序xx(进程:xxx.xxx.xxx)意外停止,请重试
解决过程:
首先参考上面的博文,将第二个button的定义修改为TextView
Button button_send = (Button)findViewById(R.id.button_send);
button_send.setOnClickListener(new tryClickListener());
TextView button2 = (TextView)findViewById(R.id.button_two);
button2.setOnClickListener(new OnClickListener(){});
这样,确实可以正常启动,没有报错。
但是有另外一个问题: 界面上只有一个button点击后有效果,button2点击后没有反应
最终,解决方法是将两个button都置成android:focusable="false",如下:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="false"
android:text="One" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:focusable="false"
android:text="Two" />
java文件还是使用Button定义控件:
Button button_send = (Button)findViewById(R.id.button_send);
button_send.setOnClickListener(new tryClickListener());
Button button2 = (Button)findViewById(R.id.button_two);
button2.setOnClickListener(new OnClickListener(){});
这样就ok啦。