0

我想编写一个代码,以便能够从标准输入中获取一个字符串并将其与一个字符串进行比较我已经在一个 Android 项目中编写了这段代码但不幸的是它停止了

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    EditText text = (EditText) findViewById(R.id.editText1);
    TextView text1 = (TextView) findViewById(R.id.textView1);

    String str = text.getText().toString();

    if(str.equals("yes"))
        text1.setText(str);
    else 
        text1.setText(0000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

谁能帮帮我吗??

提前致谢

4

2 回答 2

3

else值应该是字符串

text1.setText("0000");

您分配为整数

于 2013-11-13T17:00:10.403 回答
2

改变这个

 text1.setText(0000);
 //int value
 // setText(resid) looks for a resource with the id mentioned
 // if not found you get ResourceNotFoundException

 text1.setText(String.valueOf(0000));
 // so use String.valueOf(intvalue); 

一条建议 :

您可能希望在单击按钮时获取 edittext 的值

EditText text; 
TextView text1;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (EditText) findViewById(R.id.editText1);
    text1 = (TextView) findViewById(R.id.textView1);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

      @Override
      public void onClick(View v) {
           String str = text.getText().toString();
           if(str.equals("yes"))
         text1.setText(str);
           else 
         text1.setText(String.valueOf(0000));

      } 
    });
    }
于 2013-11-13T17:01:19.630 回答