java - 应用程序找不到 EditText

标签 java android view

EditText 是年终噩梦,我在通过 ID 查找 View 时,从 View 中获取文本时似乎返回了 null。

设置 Activity :

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() 方法。

因此上述编辑后的代码如下所示:

   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;
    } 
} 

只是一个建议:onCreate() 方法中的 setContentView 之后引用 JAVA 中的Views,并全局声明它们,以防您想在许多函数中访问它们。

希望这有帮助!

关于java - 应用程序找不到 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27724535/

相关文章:

java - 将 wicket 1.4 迁移到 1.5 - PageParameters.getAsEnum()

java - Spring MVC 3 中的异常处理以显示无效 URL 的自定义错误页面

java - Android 使用 3G 三角测量的位置坐标精度较低

Android:关于大小/分辨率独立应用程序的问题?

android - 如何旋转 GamesClient 的弹出窗口

mysql - 从两个表在mysql中创建VIEW

java - Java 中的单例

java - 将数组列表传递给 fragment ?

android - "Revoke USB debugging authorization"未在开发人员选项下列出

MySQL 自定义计算 View