我在android studio中构建了一个转换器,每次按下转换按钮时,距离部分(活动)都会崩溃。该应用程序表示特定活动已停止工作,应用程序将返回主要活动。它没有在android studio中显示任何错误,我认为我的问题可能在于使用来自微调器的int pos,但我不确定。这是我要完成的最后一个元素。请帮忙。下面是java后跟xml。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
spinnerFrom = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource(this, R.array.distance_array, android.R.layout.simple_spinner_item);
adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFrom.setAdapter(adapterFrom);
spinnerFrom.setOnItemSelectedListener(this);
spinnerTo = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource(this, R.array.distance_array, android.R.layout.simple_spinner_item);
adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTo.setAdapter(adapterTo);
spinnerTo.setOnItemSelectedListener(this);
}
public void convert(View view){
double value = new Double(distance.getText().toString());
from = spinnerFrom.getSelectedItemPosition();
to = spinnerTo.getSelectedItemPosition();
if(from == 0){
if(to == 1)
value = UnitConverter.feetToMiles(value);
else if (to == 2)
value = UnitConverter.feetToMeters(value);
else if(to == 3)
value = UnitConverter.feetToYards(value);
else
value = value;
}else if(from == 1){
if(to == 0)
value = UnitConverter.milesToFeet(value);
else if (to == 2)
value = UnitConverter.milesToMeters(value);
else if(to == 3)
value = UnitConverter.milesToYards(value);
else
value = value;
}else if(from == 2){
if(to == 0)
value =UnitConverter.metersToFeet(value);
else if(to == 1)
value = UnitConverter.metersToMiles(value);
else if(to == 3)
value = UnitConverter.metersToYards(value);
else
value = value;
}else if(from == 3){
if(to == 0)
value = UnitConverter.yardsToFeet(value);
else if(to == 1)
value = UnitConverter.yardsToMiles(value);
else if(to == 2)
value = UnitConverter.yardsToMeters(value);
else
value = value;
}
distance.setText(new Double(value).toString());
}
和xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.jacob.myapplication.Main4Activity"
android:background="@android:color/holo_blue_bright">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Distance Converter"
android:id="@+id/textViewTitle"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="92dp"
android:layout_marginStart="92dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textViewTitle"
android:layout_centerHorizontal="true"
android:hint="Enter Distance" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:spinnerMode="dialog"
android:clickable="true"
android:layout_below="@+id/textViewFrom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Convert To:"
android:id="@+id/textViewTo"
android:layout_below="@+id/spinner1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:spinnerMode="dialog"
android:layout_below="@+id/textViewTo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Convert From"
android:id="@+id/textViewFrom"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert"
android:id="@+id/buttonConvert"
android:layout_below="@+id/spinner2"
android:layout_centerHorizontal="true"
android:onClick="convert" />
答案 0 :(得分:3)
您在此处未使用相同的变量名称
double value = new Double(distance.getText()。toString());
什么是距离,距离在哪里?我无法在XML布局中看到它。
似乎你没有将EditText的名称更改为距离,在edittext中,id名称是 机器人:ID =&#34; @ + ID / EDITTEXT&#34;
因此,将距离名称更改为editText