2012-02-07 52 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