Reputation: 21
I have the most simple code and it have worked before but now for some reason it doesn't and I can't figure out why! And the problem is quite hard to google.
I am unable to get the text from the EditText
. I've created a new project, I've copied the exact code from another project (where it works) but here it just won't work. I'm hoping the problem is really obvious and i've just been staring at it for too long.
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
String artist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edit = (EditText) findViewById(R.id.artistEditText);
artist = edit.getText().toString();
}
protected void tryLetter(View v){
System.out.println(artist);
Log.d("string", artist);
System.out.println("hi");
}
}
The println
to print artist doesn't show at all and neither does the Log.d
and tryletter
is called when I press a button.
Here is the layout file if it makes a difference.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ange en artist:"
android:id="@+id/artistTextLabel"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/artistEditText"
android:layout_alignBottom="@+id/artistTextLabel"
android:layout_alignEnd="@+id/searchBtn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sök"
android:id="@+id/searchBtn"
android:onClick="tryLetter"
android:layout_below="@+id/artistEditText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Upvotes: 2
Views: 995
Reputation: 17813
In your example there are two mistakes:
Here is your code with two fixes:
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.artistEditText);
}
public void tryLetter(View v){
String artist=edit.getText().toString();
System.out.println(artist);
Log.d("string", artist);
System.out.println("hi");
}
}
Upvotes: 0
Reputation: 37404
put this artist = edit.getText().toString();
inside this tryLetter
because you need to fetch the text from EditText
when action is performed mean onClick
and make your tryLetter
method public
because it will only work when your method is
public
so use this
public void tryLetter(View v){
artist = edit.getText().toString();
System.out.println(artist);
Log.d("string", artist);
System.out.println("hi");
}
instead of this protected void tryLetter(View v){
Upvotes: 4
Reputation: 492
In simple scenarios like yours, something needs to trigger to get the data from the EditText. The best would be to place the fetch code, inside a trigger say A Button's block of code or inside a listener
Upvotes: 0