Linda
Linda

Reputation: 251

The reason that this code stops working

I wanted to write a code to be able to get a string from standard input and compare it with a String I have written this code in an Android project But unfortunately it stops

    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;
}

 }

Can Anyone please help me??

Thanks in advance

Upvotes: 0

Views: 64

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Change this

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

to

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

A suggestion :

You probably want to get the value of edittext on button click

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));

      } 
    });
    }

Upvotes: 2

Coderji
Coderji

Reputation: 7745

in else the value should be string

text1.setText("0000");

you assigned as integer

Upvotes: 3

Related Questions