Java 应用程序找不到EditText
EditText正在成为年底的噩梦,而我在按ID查找视图时,从中获取文本时似乎返回null 设置活动:Java 应用程序找不到EditText,java,android,view,Java,Android,View,EditText正在成为年底的噩梦,而我在按ID查找视图时,从中获取文本时似乎返回null 设置活动: public class Settings extends ActionBarActivity { String email; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo
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>
我不知道,我和谷歌已经为此斗争了几个小时
非常感谢您的帮助,我是新手。按照以下步骤操作:
不要在getEmail方法中使用setContentView方法
因此,上述编辑后的代码如下所示:
只是一个建议:在onCreate方法中的setContentView之后在JAVA中引用您的视图,并全局声明它们,以防您希望在许多函数中访问它们。
希望这有帮助 删除getEmailTry@android:id/email_address中的第二个setContentView请查看下面的答案@BobbleHadd这并没有解决问题,很遗憾。现在尝试在onCreate中使用视图,请发布您的logcatNope,即使在onCreate中仍然找不到EditText。是否在logcat中获取此错误。。。12-31 19:00:25.370:E/AmanatodataUpdaterHelper833:获取上下文建议失败。12-31 19:00:25.370:E/AmanatodataUpdaterHelper833:包名:com.example.testApp 12-31 19:00:25.370:E/AmanatodataUpdaterHelper833:错误消息:Disabled是否可以发布修改后的代码,所以我可以帮助你。我更改了onCreate以包括usernamet=EditTextfindViewByIdR.id.email\u地址;字符串Username=usernamet.getText.toString;Toast.makeTextthis,用户名,Toast.LENGTH\u SHORT.show;
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(){
//Removed setContentView() from this method
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;
}
}