gpt4 book ai didi

java - 如何读取旋转器并从编辑文本获取数据到 TextView ?

转载 作者:行者123 更新时间:2023-12-01 04:10:56 24 4
gpt4 key购买 nike

我想知道我的代码在哪里出错。这个 Activity 应该像一个单位转换器。有两个微调器用于选择单位,一个编辑测试用于插入值,一个 TextView 用于显示结果。实际上,当我插入一个值并按下按钮将其转换时,我会崩溃。你能帮助我吗?谢谢你,抱歉我的英语不好

public class lunghezze extends Activity implements OnClickListener {
EditText et1;
TextView tv1;
Spinner spinner1,spinner2;
Button btnAdd;

String oper = "";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,R.array.lunghezza, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,R.array.lunghezza, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);

// find the elements
et1 = (EditText) findViewById(R.id.editText1);
btnAdd = (Button) findViewById(R.id.button1);
tv1 = (TextView) findViewById(R.id.textView4);

// set a listener
btnAdd.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double num1 = 0;

double resdelta = 0;
double res = 0;
double resx2 = 0;

if (TextUtils.isEmpty(et1.getText().toString())) {
return;
}

String a = et1.getText().toString().trim();
num1 = a.isEmpty()? Double.NaN:Double.parseDouble(a);

switch (v.getId()) {
case R.id.button1:
String s1 = spinner1.getSelectedItem().toString();
String s2 = spinner2.getSelectedItem().toString();
if (s1 + s2 == "IardeIarde"){
tv1.setText("" + num1);
} else {
}
break;
default:
break;

这是日志猫

    11-12 17:56:04.063: E/AndroidRuntime(1415): FATAL EXCEPTION: main
11-12 17:56:04.063: E/AndroidRuntime(1415): java.lang.NullPointerException
11-12 17:56:04.063: E/AndroidRuntime(1415): at com.example.easyphysics.lunghezze.onClick(lunghezze.java:91)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.view.View.performClick(View.java:4084)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.view.View$PerformClick.run(View.java:16966)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.os.Handler.handleCallback(Handler.java:615)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.os.Looper.loop(Looper.java:137)
11-12 17:56:04.063: E/AndroidRuntime(1415): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 17:56:04.063: E/AndroidRuntime(1415): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:56:04.063: E/AndroidRuntime(1415): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 17:56:04.063: E/AndroidRuntime(1415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 17:56:04.063: E/AndroidRuntime(1415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 17:56:04.063: E/AndroidRuntime(1415): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您已经将 Spinner 声明为类(class)成员。所以只需在onCreate中初始化它即可。

改变

Spinner spinner1,spinner2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
... //rest of the code

Spinner spinner1,spinner2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
... //rest of the code

要比较字符串,请使用 .equals.equalsIgnoreCase 而不是 ==

关于java - 如何读取旋转器并从编辑文本获取数据到 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19936606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com