EditText返回null

时间:2014-11-26 13:42:43

标签: android

我不知道发生了什么,但所有的EditTexts都显示为NUll ..请帮忙,因为我无法得到它的原因。

public class RegisterActivity extends Activity {

String url  = ServerUrl.assignRollNumber;
LoginDataBaseAdapter loginDataBaseAdapter;
//EditText editTextUserName,editTextPassword,editTextConfirmPassword,editTextEmail,editTextPhone;
Button btnCreateAccount;
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();

    final EditText editTextUserName=(EditText)findViewById(R.id.editTextUserName);
    final EditText editTextPassword=(EditText)findViewById(R.id.editTextPassword);
    final EditText editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
    final EditText editTextEmail = (EditText)findViewById(R.id.editTextEmail);
    final EditText editTextPhone = (EditText) findViewById(R.id.editTextPhone);
    btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount); 

    btnCreateAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {  

               final String userName=editTextUserName.getText().toString();
               final String password=editTextPassword.getText().toString();
               final String emailid=editTextEmail.getText().toString();
               final String phone = editTextPassword.getText().toString();
               final String confirmPassword=editTextConfirmPassword.getText().toString();
            if(userName.equals("")||password.equals("")||confirmPassword.equals("")||emailid.equals("")||phone.equals(""))
            {
                    Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                    return;
            }
            if(!password.equals(confirmPassword))
            {
                Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                return;
            }
            else
            {
                final CharSequence[] items={"Email","Phone"};
                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                builder.setTitle("SEND USER DETAILS VIA");



                builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        if("Email".equals(items[which]))
                        {
                            Toast.makeText(getApplicationContext(), "EMAIL SELECTED", Toast.LENGTH_LONG).show();

                        }
                        else if("Phone".equals(items[which]))
                        {
                            Toast.makeText(getApplicationContext(), "PHONE SELECTED", Toast.LENGTH_LONG).show();

                        }


                    }
                });
                builder.show();

            }
        }
    });
    TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
    loginScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            finish();
        }
    });
}}

这是我的布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#ffffff">

     <!--  Header Starts-->
        <LinearLayout android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@layout/header_gradient">

            <!-- Logo Start-->
                <ImageView  android:src="@drawable/headerlogo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                />
            <!-- Logo Ends -->
        </LinearLayout>
    <!--  Header Ends -->

    <!-- Footer Start -->
             <LinearLayout android:id="@+id/footer"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="45dip"
                android:background="@layout/footer_repeat"
                android:layout_alignParentBottom="true">
            </LinearLayout>
    <!-- Footer Ends -->

    <!-- Registration Form -->
    <LinearLayout

      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="10dip"
      android:layout_below="@id/header">
      <!-- Full Name Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Full Name"/>
      <EditText android:id="@+id/editTextUserName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:singleLine="true"
            android:layout_marginBottom="20dip"/>
      <!--  Password Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Password"/>
      <EditText android:id="@+id/editTextPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:password="true"
            android:singleLine="true"
            android:layout_marginBottom="20dip"/>
      <!-- Confirm Password Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Confirm Password"/>
      <EditText android:id="@+id/editTextConfirmPassword"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:singleLine="true"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="5dip"/>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Email "/>

        <EditText android:id="@+id/editTextEmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:singleLine="true"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="5dip"/>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Phone "/>

        <EditText android:id="@+id/editTextPhone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:numeric="integer"
            android:singleLine="true"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="5dip"/>


      <!-- Register Button -->
      <Button android:id="@+id/buttonCreateAccount"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:text="Register New Account"/>
      <!-- Link to Login Screen -->
      <TextView android:id="@+id/link_to_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dip"
            android:layout_marginBottom="60dip"
            android:text="Already has account! Login here"
            android:gravity="center"
            android:textSize="20dip"
            android:textColor="#c63a29"/>

    </LinearLayout>
    <!-- Registration Form Ends -->
  </RelativeLayout>
  </ScrollView>

当我点击其中一个单选按钮或甚至点击注册按钮

时,它总是变为空值

3 个答案:

答案 0 :(得分:0)

尝试取下.toString();在getText()之后。

答案 1 :(得分:0)

您可以使用if(!TextUtils.isEmpty(userName.getText()));或尝试此操作:  String userName =!TextUtils.isEmpty(userNamee.getText())?editTextUserName.getText()。toString():&#34;&#34;;

答案 2 :(得分:0)

您的活动与xml文件之间没有任何关联: 将tools:context="com.example.<applicationname>.RegisterActivity"添加到您的xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
tools:context="com.example.<applicationname>.RegisterActivity">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#ffffff">

     <!--  Header Starts-->