我想比较两个Spinner值,根据它们,我想显示一个结果 我尝试了以下代码,但每次运行应用程序时都不会显示预期的结果。
如何比较这些值?
这是代码
public class MyApp extends Activity implements OnItemSelectedListener {
Spinner temp1,temp2;
TextView t1,t2,t3;
EditText itemp;
Integer i,o,conv;
Double tc,tk,tf;
String sel1,sel2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
t3=(TextView)findViewById(R.id.otemp);
itemp=(EditText)findViewById(R.id.itemp);
temp1=(Spinner)findViewById(R.id.temp);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp1.setAdapter(adapter);
temp1.setOnItemSelectedListener(this);
temp2=(Spinner)findViewById(R.id.temp2);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp2.setAdapter(adapter2);
temp2.setOnItemSelectedListener(this);
tk=Double.parseDouble(itemp.getText().toString());
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
Spinner temp1=(Spinner)parent;
Spinner temp2=(Spinner)parent;
if (temp1.getId()==R.id.temp) {
String item1 = parent.getItemAtPosition(position).toString();
t1.setText(item1);
sel1=item1;
}
if (temp2.getId()==R.id.temp2) {
String item2 = parent.getItemAtPosition(position).toString();
t2.setText(item2);
sel2=item2;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
当我删除这些行时,应用程序可以正常工作,但我需要比较这些Spinner值
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
这个实现有问题吗?
logcat的
08-04 14:48:28.651 13541-13541/com.combud.calcthree.compencalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.combud.calcthree.compencalculator/com.combud.calcthree.compenc alculator.MyApp}: java.lang.NumberFormatException: Invalid double: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:248)
at java.lang.Double.parseDouble(Double.java:295)
at com.combud.calcthree.compencalculator.MyApp.onCreate(MyApp.java:50)
at android.app.Activity.performCreate(Activity.java:4469)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
答案 0 :(得分:2)
var delegate = functionToBeDelegated;
functionToBeDelegated = function () {
//
// Add functionality to the function
//
// run the old version of the function in native scope
return delegate.apply(this, arguments);
};
使用以下代码更新您的代码。你在申请表中想做什么告诉我你的要求没有写你的意思如果在那里声明
答案 1 :(得分:1)
这是由于您的sel1
和sel2
字符串在
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
并且您正在尝试检查null对象上的值
这样做
String sel1 = "",sel2 = "";
在检查它们之前还要添加它们的值,如
sel1= temp1.getSelectedItem().toString();
sel2= temp2.getSelectedItem().toString();
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
如果要在选择微调器之后更新这些值,请在onItemSelected
微调器方法中添加上面的行而不是onCreate
方法
如果有帮助,请告诉我
修改强>
根据你的logcat异常是由于
Caused by: java.lang.NumberFormatException: Invalid double: ""
在第
行tk=Double.parseDouble(itemp.getText().toString());
这是由于编辑文本itemp
没有任何值,但您仍在访问数据并将其解析为double,这会导致该异常。
因此,在解析值之前,请检查编辑文本的值,如
String textValue = itemp.getText().toString();
if(textValue != null && !textValue.equalIgnoreCase("")){
tk=Double.parseDouble(textValue);
}