如何阅读微调器并将编辑文本中的数据导入到textview中?

时间:2013-11-12 18:04:08

标签: java android android-layout android-edittext spinner

我想知道我的代码出错的地方。这个活动应该像一个单位转换器。有两个微调器可以选择单元,一个用于插入值的edittest和一个用于显示结果的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;

这里是logcat

    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)

1 个答案:

答案 0 :(得分:0)

您已经将Spinner声明为类成员。所以只需在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代替==