EditText является кошмаром конца года, и я, когда обнаруживаю вид по ID, кажется, возвращается с нулевым значением при получении текста из него.Приложение не находит EditText
Настройки Активность:
public class Settings extends ActionBarActivity {
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
createPreferences();
}
public void createPreferences(){
SharedPreferences settings = getSharedPreferences("emailAddress", 0);
getData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
public void getData(){
email = getEmail();
}
public String getEmail(){
setContentView(R.layout.activity_settings);
EditText usernameEditText = (EditText)findViewById(R.id.email_address);
String Username = usernameEditText.getText().toString();
if (Username.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return Username;
}
System.out.println(email + " 8888888888888888888");
if(Username !=null){
email = Username;
}
if (email==null){
SharedPreferences setting = getSharedPreferences("emailAddress", Context.MODE_PRIVATE);
//check preferences.
String emailNew = setting.getString("emailAddress", "n").toString();
if(emailNew != null){
email = emailNew;
}
if(email==null){
//shit
}
}
if(email!= null){
}
return email;
}
}
Настройки XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.keepsafe.Settings" >
<EditText
android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/enteremail"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/password_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/email_address"
android:layout_alignRight="@+id/email_address"
android:layout_below="@+id/email_address"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter password"
android:inputType="textPassword" />
<EditText
android:id="@+id/email_address_toSendto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/enteremail"
android:inputType="textEmailAddress"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/password_enter"
android:layout_below="@+id/password_enter"
android:layout_marginLeft="14dp"
android:layout_marginTop="29dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/SettingsSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/password_enter"
android:layout_alignRight="@+id/password_enter"
android:layout_centerVertical="true"
android:onClick="dataSubmit"
android:text="Submit" />
</RelativeLayout>
Я понятия не имею, и я боролся с этим Google в течение нескольких часов.
Любая помощь приветствуется, я новичок в этом.
удалить этот второй '' setContentView' внутри getEmail' – Blundell
Попробуйте @android: ID/email_adress – MrWasdennnoch
Пожалуйста, обратите внимание на ответ ниже @BobbleHead –