2012-02-07 100 views
3

我是一個完整的初學者程序員(在Android中)。我對在Android中創建監聽器的概念感到困惑。我的程序的目的很簡單:在單位之間轉換(如華氏溫度,攝氏溫度,華氏溫度等)。如何獲取EditText對象中的文本,然後如何打印出來?每次運行時我都會收到一個錯誤(不幸的是,Converter.exe已停止。)以下是我的代碼。請指教。我有兩個佈局(.xml)文件(我不知道這是否是正確的編程風格)。如何從EditText處理華氏溫度並打印相應的攝氏溫度?

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 VM 
02-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: main 
02-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.NullPointerException 
02-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.access$600(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.NullPointerException 
02-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 
+0

什麼錯誤? 'getText(0)'不對。使用'getText()。toString()'。 – adneal 2012-02-07 01:05:13

+0

aneal,我的問題是問如果我的代碼是正確的?我糾正了你提到的錯誤(謝謝你)。你有什麼特別的錯誤嗎? – sameetandpotatoes 2012-02-07 01:38:29

+0

當你點擊你的按鈕時會發生什麼?你有日誌嗎?這些是有用的事情要提及和上傳。 – adneal 2012-02-07 01:42:00

回答

4

這是非常簡單的,我想你可以從這裏找到答案。

// 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 celcius 
private float convertFahrenheitToCelcius(float fahrenheit) { 
    return ((fahrenheit - 32) * 5/9); 
} 

// Converts to fahrenheit 
private float convertCelciusToFahrenheit(float celsius) { 
    return ((celsius * 9)/5) + 32; 
} 
+0

非常感謝您的幫助。對此,我真的非常感激。我會從這裏開始工作,看看我能否更多地瞭解這個程序的語法。 (也可以擴展它:)) – sameetandpotatoes 2012-02-07 02:54:58

+0

請確保你的答案是正確的,請。 – adneal 2012-02-07 02:56:28

0

只是一個例子。我前一段時間寫了這個示例temp轉換器。也許有人會覺得它有用。


public class TempTest extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout ll = new LinearLayout(this);   
     ll.setOrientation(1); 
     Button b = new Button(this); 
     b.setText("get temperature"); 
     b.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view) { 
        getCelciusOrFarenheit(); 
      } 
     }); 
     ll.addView(b); 
     setContentView(ll); 
    } 

    public AlertDialog getCelciusOrFarenheit(){ 
    AlertDialog.Builder cof = new AlertDialog.Builder(this); 
    cof.setTitle("Select Temperature Conversion"); 
    String[] types = {"To Celcius", "To Farenheit"}; 
    cof.setItems(types, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
      switch(which){ 
      case 0: 
       createTempDialog(true).show(); 
       break; 
      case 1: 
       createTempDialog(false).show(); 
       break; 
      } 
     } 
    }); 
    return cof.show(); 
    } 
    public AlertDialog createTempDialog(final boolean cf){ 
     final EditText et = new EditText(this); 
     et.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     final Formatter f = new Formatter(); 
     return new AlertDialog.Builder(this).setView(et).setTitle(cf?"Enter Farenheit temperature":"Enter Celcius temperature").setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialogInterface, int i) { 
       double answer = 0; 
       if (cf) { 
        answer = (Double.parseDouble(et.getText().length() > 1 ? et.getText().toString() : "0") - 32) * 5/9; 
       } else { 
        answer = Double.parseDouble(et.getText().length() > 1 ? et.getText().toString() : "0")*9/5 + 32; 
       } 
       new AlertDialog.Builder(TempTest.this).setMessage(f.format("%.2f", answer).toString()).setTitle(cf?"Celcius":"Farenheit") 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialogInterface, int i) { 
           dialogInterface.dismiss(); 
          } 
         }) 
         .create().show(); 
      } 
     }).create(); 
    } 

} 
+0

哇,這看起來很混亂。另外,我試着在我的電腦上運行它,它給了我一個f.format的編譯器錯誤。 – sameetandpotatoes 2012-02-07 02:20:48

+0

我剛剛運行它沒有任何問題隊友。我不會發布它,如果我不是一個工作樣本。您可能導入了錯誤的格式化程序。 – 2012-02-07 02:26:02

+0

是的,它可能是我的錯。抱歉。但你能幫我嗎? – sameetandpotatoes 2012-02-07 02:27:48

相關問題