我在一个正在Android上开发应用程序的项目中。在这里,我必须找到两个定义为字符串的数字之间的总和,然后将它们显示在textview上。但是我的应用程序崩溃了,下面是我的代码。最佳吗?请帮助我,我必须尽快提交。
String price=Place_View.prices[spinner1.getSelectedItemPosition()];
String fare=Cars.SUVFare[spinner2.getSelectedItemPosition()];
String result="";
int calc1=Integer.parseInt(price);
int calc2=Integer.parseInt(fare);
int calc3=calc1+calc2;
result=Integer.toString(calc3);
Price.setText(result);
}
});
spinner1:
public class Place_View {
public static final String[] locName = {"Sivasagar", "Jorhat", "Tinsukia", "Mohanbari", "Pasighat", "Digboi", "Majuli"};
public static final String[] prices = {"2625.00","3200.00","2000.00","500.00","4000.00","2800.00","4000.00"};
}
旋转者2:
public class Cars {
public static final String[] SUV = {"Mahindra XUV", "Renault Duster", "Hyundai Creta", "Nissan Terrano", "Maruti Suzuki Brezza"};
public static final String[] SUVFare={"1000","800","800","750","650"};
}
现在,当我选择字符串价格和SUVFare两者之一时,必须将它们的值相加,然后将它们放在TextView Price上。 不知道如何发布logcat,所以放上我自定义的类。
答案 0 :(得分:0)
尝试一下:
result=String.valueOf(calc3);
代替
result=Integer.toString(calc3);
答案 1 :(得分:0)
请尝试在Android中将style.width
转换为table
:
int
答案 2 :(得分:0)
String price=Place_View.prices[spinner1.getSelectedItemPosition()];
String fare=Cars.SUVFare[spinner2.getSelectedItemPosition()];
String result="";
int calc1 = 0;
int calc2 = 0;
try {
calc1 = Integer.parseInt(price);
calc2 = Integer.parseInt(fare);
}catch(NumberFormatException e) {
Log.e("Error","price or fare are not a Number in string format");
}
int calc3=calc1+calc2;
result=String.valueOf(calc3);
Price.setText(result);
可能是您从微调器中提取的String值不是数字,而是通用的非数字字符串,在使用Integer.parseInt进行解析时可能会崩溃,只需为NumberFormatException添加一个catch即可避免运行时崩溃。 并根据您的用例显示一些消息或对您的应用程序合适的内容来处理异常。
答案 3 :(得分:0)
新答案
您可能正在尝试执行以下操作:
String price = Place_View.prices[spinner1.getSelectedItemPosition()];
String fare = Cars.SUVFare[spinner2.getSelectedItemPosition()];
String result="";
float calc1=Float.parseFloat(price); //Note: float
int calc2=Integer.parseInt(fare);
float calc3=calc1+calc2; //Note: float
result=Float.toString(calc3); //Note: Float
Price.setText(result);
完整示例:
public class MainActivity extends AppCompatActivity {
TextView Price;
Spinner spinner1;
Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Price = findViewById(R.id.tvPrice);
spinner1 = findViewById(R.id.spinner1);
spinner2 = findViewById(R.id.spinner2);
ArrayAdapter<String> adapterLocation = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, Place_View.locName);
adapterLocation.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapterLocation);
ArrayAdapter<String> adapterCar = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, Cars.SUV);
adapterCar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterCar);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String price = Place_View.prices[spinner1.getSelectedItemPosition()];
String fare = Cars.SUVFare[spinner2.getSelectedItemPosition()];
String result="";
float calc1=Float.parseFloat(price);
int calc2=Integer.parseInt(fare);
float calc3=calc1+calc2;
result=Float.toString(calc3);
Price.setText(result);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String price = Place_View.prices[spinner1.getSelectedItemPosition()];
String fare = Cars.SUVFare[spinner2.getSelectedItemPosition()];
String result="";
float calc1=Float.parseFloat(price);
int calc2=Integer.parseInt(fare);
float calc3=calc1+calc2;
result=Float.toString(calc3);
Price.setText(result);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
旧答案
正如快速学习者所评论的,如果您将错误发布到logcat中,我们可能会更有帮助。
关于您的代码,我有几个问题。
确定要使用:
getSelectedItemPosition()
因为这为您提供了所单击项目的位置(第一个项目为0,第二个项目为1,依此类推)
getItemAtPosition(position)
以上功能将为您提供点击位置的值。
我不完全了解此行的作用
String price=Place_View.prices[spinner1.getSelectedItemPosition()];
您是否在视图内引用数组?