2012-02-07 56 views
0

我開發了一個簡單的單位換算器,並且我被困在2個問題上。 第一個是我編譯和運行APK後,應用程序崩潰點擊轉換按鈕。 第二個是我不知道如何連接forResult(計算後的答案變量)到Text「After」旁邊的框中。 我真的乞求你的幫助....謝謝.... ^^單位換算的問題

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:gravity="left|right" 
android:orientation="vertical" 
android:background="#66FFFF" >  

<FrameLayout 
    android:id="@+id/frameLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="20dp" > 
</FrameLayout> 

<TextView 
    android:id="@+id/UnitText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/UnitText" 
    android:textColor="@android:color/black" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Spinner 
    android:id="@+id/Spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:prompt="@string/UnitPrompt"/> 

<FrameLayout 
    android:id="@+id/frameLayout3" 
    android:layout_width="match_parent" 
    android:layout_height="20dp" > 
</FrameLayout> 


<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:orientation="vertical" > 


    <Button 
     android:onClick="converter" 
     android:id="@+id/ConvertButton" 
     android:layout_width="131dp" 
     android:layout_height="wrap_content" 
     android:text="@string/ConvertText" /> 

</LinearLayout> 

<FrameLayout 
    android:id="@+id/frameLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="30dip" > 
</FrameLayout> 

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <EditText 
      android:id="@+id/BeforeBox" 
      android:layout_width="250dp" 
      android:layout_height="wrap_content" 
      android:inputType="numberDecimal" /> 

     <TextView 
      android:id="@+id/BeforeText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/BeforeText" 
      android:textColor="@android:color/black" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <EditText 
      android:id="@+id/AfterBox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="numberDecimal" /> 

     <TextView 
      android:id="@+id/AfterText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/AfterText" 
      android:textColor="@android:color/black" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </TableRow> 
</TableLayout> 

BasicUnitconverterActivity.Java 

package arirang.unit.converter; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Button; 
import android.widget.TextView; 
public class BasicUnitConverterActivity extends Activity { 

private double val1; 
Spinner Type; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1); 
    ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
      this, R.array.UnitList, android.R.layout.simple_spinner_item); 
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner1.setAdapter(adapter1); 
} 



public void converter() { 
    Type = (Spinner)findViewById(R.id.Spinner1); 
    val1 = (R.id.BeforeBox); 
    double dbResult = 0; 

    if(Type.getSelectedItem().toString().equals("meter to inch")) 
    { 
     dbResult = val1 * 39.3700787 ; 
    } 

    else if(Type.getSelectedItem().toString().equals("meter to feet")) 
    { 
     dbResult = val1 * 3.2808399; 
    } 

    else if(Type.getSelectedItem().toString().equals("feet to inch")) 
    { 
     dbResult = val1 * 12; 
    } 

    else if(Type.getSelectedItem().toString().equals("feet to meter")) 
    { 
     dbResult = val1/3.2808399; 
    } 

    else if(Type.getSelectedItem().toString().equals("inch to meter")) 
    { 
     dbResult = val1/39.3700787 ; 
    } 

    else if(Type.getSelectedItem().toString().equals("inch to feet")) 
    { 
     dbResult = val1/12; 
    } 

    else if(Type.getSelectedItem().toString().equals("kilogram to pound")) 
    { 
     dbResult = val1 * 2.20462262; 
    } 

    else if(Type.getSelectedItem().toString().equals("pound to kilogram")) 
    { 
     dbResult = val1/2.20462262; 
    } 

    else if(Type.getSelectedItem().toString().equals("Fahernheit to Celsius")) 
    { 
     dbResult=(val1 - 32)/(5/9); 
    } 

    else if(Type.getSelectedItem().toString().equals("Celsius to Fahernheit")) 
    { 
     dbResult = (9/5) * val1 + 32; 
    } 

    TextView ShowBox = (TextView)findViewById(R.id.AfterBox); 
    ShowBox.setText("forResult"); 
} 
} 

回答

0

有幾個重要的信息,你離開了。一個是崩潰的logcat信息。另一種是點擊Convert按鈕時的onClick方法。

我有一個建議給你。如果這沒有幫助,請發佈更多信息。

我打算假設單擊Convert按鈕最終調用您已列出的轉換器方法。在這種方法中,您具備以下條件:

val1 = (R.id.BeforeBox); 

您然後在隨後的計算中使用VAL1。這行代碼將爲您提供BeforeBox的ID,而不是該EditText的內容。我不確定這是否足以導致崩潰或是否會給你帶來奇怪的結果。

+0

我用android:onClick =「轉換器」...但是我需要用什麼命令或代碼從BeforeBox中獲取數字到val1? – user1193653 2012-02-08 00:02:00

+0

BeforeBox是一個EditText,所以你從EditText開始。t =(EditText)findViewById(R.id.BeforeBox);然後,您可以使用String s = t.getText()。toString();來檢索文本。然後你可以做Double d(s)這樣的事情;最後val1 = s.doubleValue(); - 抱歉格式在這裏,但我不能在此評論框中插入換行符。 – 2012-02-08 03:05:59