应用程序找不到EditText

时间:2014-12-31 17:47:07

标签: java android view

EditText是年终噩梦,我在按ID查找视图时,从中获取文本时似乎返回null。

设置活动:

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>

我不知道,我一直在谷歌与谷歌斗争几个小时。

感谢任何帮助,我是新手。

1 个答案:

答案 0 :(得分:0)

按照以下步骤操作:

请勿在{{1​​}}方法中使用setContentView()方法。

因此,上述修改后的代码如下所示:

getEmail()

只是一个建议:在您的 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; } } 方法Views之后的JAVA中引用setContentView,并在全球范围内声明这些onCreate()以防万一在许多功能中访问它们。

希望这有帮助!