0

Hello i have problem with getting value from EditText field

My code is:

public class GameSetupActivity extends Activity {

DatePicker datepicker; 
TimePicker timepicker;
DatabaseManager dbm = new DatabaseManager(this);
public EditText enemy_name;
public EditText date;
public EditText time;
public EditText place;
public String enemy_name_s;
public String date_s;
public String time_s;
public String place_s;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_setup);
    enemy_name = (EditText) findViewById(R.id.enemy_name_activity_game_setup );
    date = (EditText) findViewById(R.id.date);
    time = (EditText) findViewById(R.id.time);
    place = (EditText) findViewById(R.id.place);
    this.setTitle(getResources().getString(R.string.preparing_game));

}

public void chose_players_button(View v)
{
    enemy_name_s=enemy_name.getText().toString();
    date_s=date.getText().toString();
    time_s = time.getText().toString();
    place_s=place.getText().toString();
    Intent intent = new Intent(GameSetupActivity.this,ChosePlayersForGameActivity.class);       
    startActivity(intent);
}

and layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent" 
android:fillViewport="true"
android:background="@drawable/boisko_610"


android:gravity="top"
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=".GameSetupActivity" 
 >

<EditText
    android:id="@+id/enemy_name_activity_game_setup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:hint="@string/enemy_team_name" />

<Button
    android:id="@+id/chose_players_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/enemy_name_activity_game_setup"
    android:layout_below="@+id/enemy_name_activity_game_setup"
    android:layout_marginTop="22dp"
    android:onClick="chose_players_button"
    android:text="@string/chose_players" />

but when i debug it every time enemy_name_s is empty, even if i use enemy_name.setText("something") field in app is empty and i have no idea whats wrong, please help.

Problem is not that method public void chose_players_button(View v) is not working, it does. its the line enemy_name_s=enemy_name.getText().toString() is not working

  • 1
    Did place a breakpoint after " enemy_name_s=enemy_name.getText().toString();" to check if enemy_name_s is really "null" or if enemy_name.getText() is really returning null? – nunofmendes Nov 01 '14 at 12:02
  • while debugging after enemy_name_s=enemy_name.getText().toString() enemy_name_s was "" but field in ap had something wroten in it – user7787295 Nov 01 '14 at 12:09
  • i added Log.i("BUTTON PRESSED", enemy_name.getText().toString()); Log.i("error", "something"); in chose_players_button but in LogCat there is only error something log – user7787295 Nov 01 '14 at 12:40
  • i think that enemy_name = (EditText)findViewById(R.id.findViewById(R.id.enemy_name_activity_game_setup ); somehow doesnt connect enemy_name to field in layout but i have no ide what to do with this – user7787295 Nov 01 '14 at 12:43
  • http://zapodaj.net/0d5e574fe559e.jpg.html (i cant yes add images here cause i am new user) – user7787295 Nov 01 '14 at 12:59
  • there might be something wrong with your project files because your code runs fine when i test it – Shahzad Nov 01 '14 at 13:21
  • clean you project and run again – codePG Nov 01 '14 at 13:45
  • did it, still same problem – user7787295 Nov 01 '14 at 13:52

2 Answers2

0

I try your code with little editting belowe and it is running fine, send next parts of java and xml, There is some problem.

public class MainActivity extends Activity {

DatePicker datepicker; 
TimePicker timepicker;
public EditText enemy_name;
public EditText date;
public EditText time;
public EditText place;
public String enemy_name_s;
public String date_s;
public String time_s;
public String place_s;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_setup);
enemy_name = (EditText) findViewById(R.id.enemy_name_activity_game_setup );
//date = (EditText) findViewById(R.id.date); it is not in your xml that you sent
//time = (EditText) findViewById(R.id.time);
//place = (EditText) findViewById(R.id.place);

}

public void chose_players_button(View v)
{
enemy_name_s=enemy_name.getText().toString();
//date_s=date.getText().toString();
//time_s = time.getText().toString();
//place_s=place.getText().toString();

Toast.makeText(this, enemy_name_s, Toast.LENGTH_SHORT).show();
}


}

and my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent" 
android:fillViewport="true"
android:gravity="top"
tools:context=".GameSetupActivity" 
 >

<EditText
android:id="@+id/enemy_name_activity_game_setup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>

<Button
android:id="@+id/chose_players_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/enemy_name_activity_game_setup"
android:layout_below="@+id/enemy_name_activity_game_setup"
android:layout_marginTop="22dp"
android:onClick="chose_players_button"/>

</RelativeLayout>
eurosecom
  • 2,932
  • 4
  • 24
  • 38
0

It must have been something with project files cause i made a new project and copy other calsses and it changing then a bot and it worked fine.