我正在尝试创建一个转换温度的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>
这是我的logcat
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
答案 0 :(得分:1)
你错过了一个buttonClicked方法。您只有按钮点击您发布的内容。
答案 1 :(得分:0)
以xml的形式,你有这样的东西吗?
<RadioGroup>
<RadioButton android:onClick="buttonClick">
<RadioButton android:onClick="buttonClick">
</RadioGroup>
如果是这样,android会自动互相排斥按钮。
强制关闭通常会导致logcat中的消息。您是否作为调试器连接?或者你能在IDE中看到logcat吗?
为了避免例外情况,我会在parseDouble
中围绕您的try
来电:
try{
num=Double.parseDouble(in.getText().toString());
calc ....
out.setText ...
} catch (NumberFormatException) { // what to do if there's no number?
out.setText("enter a number");
}