Patzu Patzu - 10 days ago 5
Android Question

cannot pass the editView content to a method why?

There is an

EditText
and a
Button
inside an
Activity
, After entering some text inside an
EditText
and clicking button, a method is called and passes content of
EditText
to a method like below:

public class MainActivity extends AppCompatActivity {

TextView txt_title;
Button btn_send;
EditText edt_notification;
public final static String NOTIFICATION_DATA="NOTIFICATION_DATA";

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

txt_title= (TextView) findViewById(R.id.txt_title);
edt_notification= (EditText) findViewById(R.id.edt_notification);
btn_send= (Button) findViewById(R.id.btn_send);

btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendNotification(Calendar.getInstance().getTimeInMillis(), edt_notification.getText().toString());

}
});


}

private void SendNotification(long timeInMillis, String s) {
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

}

}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ir.wikichera.badamchi.sendnotification.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_title"
android:text="Notification Text:"
android:textSize="18sp"
android:textStyle="normal|bold" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/txt_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/edt_notification"
android:hint="put your text here"
android:singleLine="false" />

<Button
android:text="Send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edt_notification"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="17dp"
android:id="@+id/btn_send" />
</RelativeLayout>


The problem is variable "s" is all the time giving me "Cannot Find Local Variable 's'". why? This is a simple issue but I can't understand. please explain?

enter image description here

Answer

May be you are using AndroidStudio that has instant run enabled. Try after turning it off. Because when it is enabled our code changes doesn't take place sometimes. So try after turning it off :

File → Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run.

Comments