gpt4 book ai didi

java - 我不能选择我创建的其他单选按钮?

转载 作者:行者123 更新时间:2023-11-30 02:40:08 26 4
gpt4 key购买 nike

我正在尝试创建一个转换温度的 Android 应用程序。我的应用大部分都能正常工作,但我无法选择其他 RadioButton。我的应用程序关闭。我认为当我点击一个时,我需要将另一个 RadioButton 设置为 false。我不知道该怎么办。

package com.example.temperatureconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.*;

public class Calculate extends Activity
{
private EditText in, out;
//******************onCreate()*****************************
@Override
protected void onCreate(Bundle b)
{
super.onCreate(b);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_calculate);

in = (EditText)findViewById(R.id.input);
out =(EditText)findViewById(R.id.results);

in.setOnKeyListener(new View.OnKeyListener()
{

@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
double num, calc;
if((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
if(v.getId() == R.id.radio1)
{
num = Double.parseDouble(in.getText().toString());
calc = ((9.0/5.0) * num + 32);
out.setText(String.valueOf(calc));
}
else
{
num = Double.parseDouble(in.getText().toString());
calc = (num - 32) * (9.0/5.0);
out.setText(String.valueOf(calc));
}
return true;
}
return false;
}
});
}
//******************buttonClick()*****************************
public void buttonClick(View v)
{
boolean checked = ((RadioButton)v).isChecked();
switch(v.getId())
{
case R.id.radio1:
if(checked)
{

}

break;
case R.id.radio2:
if(checked)
{

}
break;
}
}
}

这是我的布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/celsius"
android:onClick="buttonClicked"
android:checked="true"/>
<RadioButton android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/farenheit"
android:onClick="buttonClicked"/>
</RadioGroup>

<EditText android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<EditText android:id="@+id/results"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>

这是我的日志

09-17 18:23:06.565: D/gralloc_goldfish(2225): Emulator without GPU emulation detected.
09-17 18:23:11.775: D/AndroidRuntime(2225): Shutting down VM
09-17 18:23:11.775: W/dalvikvm(2225): threadid=1: thread exiting with uncaught exception (group=0xb3abcba8)
09-17 18:23:11.855: E/AndroidRuntime(2225): FATAL EXCEPTION: main
09-17 18:23:11.855: E/AndroidRuntime(2225): Process: com.example.temperatureconverter, PID: 2225
09-17 18:23:11.855: E/AndroidRuntime(2225): java.lang.IllegalStateException: Could not find a method buttonClicked(View) in the activity class com.example.temperatureconverter.Calculate for onClick handler on view class android.widget.RadioButton with id 'radio2'
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.view.View$1.onClick(View.java:3810)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.view.View.performClick(View.java:4438)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.view.View$PerformClick.run(View.java:18422)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.os.Handler.handleCallback(Handler.java:733)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.os.Looper.loop(Looper.java:136)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-17 18:23:11.855: E/AndroidRuntime(2225): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 18:23:11.855: E/AndroidRuntime(2225): at java.lang.reflect.Method.invoke(Method.java:515)
09-17 18:23:11.855: E/AndroidRuntime(2225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-17 18:23:11.855: E/AndroidRuntime(2225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-17 18:23:11.855: E/AndroidRuntime(2225): at dalvik.system.NativeStart.main(Native Method)
09-17 18:23:11.855: E/AndroidRuntime(2225): Caused by: java.lang.NoSuchMethodException: buttonClicked [class android.view.View]
09-17 18:23:11.855: E/AndroidRuntime(2225): at java.lang.Class.getConstructorOrMethod(Class.java:472)
09-17 18:23:11.855: E/AndroidRuntime(2225): at java.lang.Class.getMethod(Class.java:857)
09-17 18:23:11.855: E/AndroidRuntime(2225): at android.view.View$1.onClick(View.java:3803)
09-17 18:23:11.855: E/AndroidRuntime(2225): ... 12 more
09-17 18:23:14.565: I/Process(2225): Sending signal. PID: 2225 SIG: 9

最佳答案

您缺少 buttonClicked 方法。您仅从发布的内容中获得 buttonClick。

关于java - 我不能选择我创建的其他单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25901072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com