android提示:应用程序xx(进程:xxx.xxx.xxx)意外停止,请重试

本文解决了Android应用中两个按钮点击触发不同Activity时出现的问题。通过设置按钮的focusable属性为false并确保按钮类型一致,实现了两个按钮点击事件的正确响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天遇到这个问题,参考了博文 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啦。




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值