我对在Android中创建监听器的概念感到困惑.我的程序的目的很简单:在单位之间进行转换(如华氏温度到摄氏温度,摄氏温度到华氏温度等).如何获取EditText对象中的文本,然后如何将其打印出来?我每次运行时都会收到错误(不幸的是,Converter.exe已停止.)这是我的代码到目前为止.请指教.
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;public class Converteractivity extends Activity{/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); button button = (button) findVIEwByID(R.ID.temp_button); button.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { setContentVIEw(R.layout.body); } }); button ok = (button) findVIEwByID(R.ID.OK_button); ok.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { EditText et = (EditText) getText(0); final int num = Integer.parseInt(et.toString()); double answer = (num - 32) * 5/9; TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.editText); tv.setText(Double.toString(answer)); } });}}
我也有两个布局(.xml)文件(我不确定这是否是正确的编程风格).
main只显示可以单击的按钮.然后,我点击按钮后出现第二个.xml文件.
main.xml中
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:orIEntation="vertical" ><button androID:ID="@+ID/temp_button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/F" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/C" /><button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/I" /><button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Cm" /> </linearLayout>
body.xml(第二个) –
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content"androID:orIEntation="vertical" ><TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:text="Enter temperature:" /><EditText androID:ID="@+ID/editText" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:background="@androID:drawable/editBox_background" androID:digits="1, 2, 3, 4, 5, 6, 7, 8, 9" /><button androID:ID="@+ID/cancel_button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Cancel" /><button androID:ID="@+ID/OK_button" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="OK" /> </linearLayout>
重申一下,我的问题出现在这段代码中:
button ok = (button) findVIEwByID(R.ID.OK_button); ok.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { EditText et = (EditText) getText(0); final int num = Integer.parseInt(et.toString()); double answer = (num - 32) * 5/9; TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.editText); tv.setText(Double.toString(answer)); } });
目的是从EditText对象中获取用户输入的数字,转换它并将其打印出来,但我不确定我是否正确,或者这是否在正确的位置.
**更新:这是日志.
02-06 19:53:36.148: D/AndroIDRuntime(529): Shutting down VM02-06 19:53:36.158: W/dalvikvm(529): threadID=1: thread exiting with uncaught exception (group=0x409c01f8)02-06 19:53:36.168: E/AndroIDRuntime(529): FATAL EXCEPTION: main02-06 19:53:36.168: E/AndroIDRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapra.converter/com.sapra.converter.Converteractivity}: java.lang.NullPointerException02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1956)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread.access0(ActivityThread.java:123)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.os.Handler.dispatchMessage(Handler.java:99)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.os.Looper.loop(Looper.java:137)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread.main(ActivityThread.java:4424)02-06 19:53:36.168: E/AndroIDRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)02-06 19:53:36.168: E/AndroIDRuntime(529): at java.lang.reflect.Method.invoke(Method.java:511)02-06 19:53:36.168: E/AndroIDRuntime(529): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)02-06 19:53:36.168: E/AndroIDRuntime(529): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)02-06 19:53:36.168: E/AndroIDRuntime(529): at dalvik.system.NativeStart.main(Native Method)02-06 19:53:36.168: E/AndroIDRuntime(529): Caused by: java.lang.NullPointerException02-06 19:53:36.168: E/AndroIDRuntime(529): at com.sapra.converter.Converteractivity.onCreate(Converteractivity.java:29)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.Activity.performCreate(Activity.java:4465)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)02-06 19:53:36.168: E/AndroIDRuntime(529): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1920)02-06 19:53:36.168: E/AndroIDRuntime(529): ... 11 more
解决方法:
这很简单,我想你可以从这里弄明白.
// This is the TextVIEw you set in your layout. It will display the temperature. final TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.tv); // This sets up the EditText so you can capture the input and return the temperature. final EditText edittext = (EditText) findVIEwByID(R.ID.et); edittext.setonKeyListener(new OnKeyListener() { public boolean onKey(VIEw v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // Perform action on key press float f = float.parsefloat(edittext.getText().toString()); tv.setText(String.valueOf(convertCelciusToFahrenheit(f))); return true; } return false; } });}// Converts to celciusprivate float convertFahrenheitToCelcius(float fahrenheit) { return ((fahrenheit - 32) * 5 / 9);}// Converts to fahrenheitprivate float convertCelciusToFahrenheit(float celsius) { return ((celsius * 9) / 5) + 32;}
总结 以上是内存溢出为你收集整理的android – 如何从EditText处理华氏温度并打印其相应的摄氏温度?全部内容,希望文章能够帮你解决android – 如何从EditText处理华氏温度并打印其相应的摄氏温度?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)