to send a message in my application in android i have used the following code.
package wishme.code;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity{
public void onCreate(Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
setContentView(R.layout.main2);
Button previous=(Button) findViewById(R.id.button_prev);
Button send=(Button) findViewById(R.id.button_send);
EditText ph_no=(EditText) findViewById(R.id.edit_phone);
EditText msg=(EditText) findViewById(R.id.edit_msg);
}
public void prevclickhandler(View view)
{
switch(view.getId())
{
case R.id.button_prev:
Intent intent=new Intent();
setResult(RESULT_OK,intent);
finish();
break;
case R.id.button_send:
**String ph_no= ph_no.getText().tostring();**
}}
}
But the line highlighted gives error that is
the method getText is undefined for the type string
how can i resolve this.
Your variables are defined in different scopes. The ph_no EditText is defined in onCreate and not available in your click handler. The ph_no you are actually calling getText() on is the String you are defining.
Instead, you can define the EditText inside your Activity as a instance variable and rename your String so the variable names don’t collide.
Another approach is to just call findViewById when you need access to the EditText, like so: