5
我已經在android studio中構建了一個轉換器,並且每次按下轉換按鈕時距離部分(活動)都會崩潰。該應用程序表示特定活動已停止工作,並且該應用程序返回到「主要」活動。它沒有在android studio中顯示任何錯誤,我認爲我的問題可能是使用微調框中的int pos,但我不確定。這是完成我的最後一個元素。請幫忙。下面是java之後的xml。Android轉換器應用程序崩潰轉換
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" />
就是這樣。感謝銳利的眼睛 –