TextInputLayout是否为null对象?

时间:2016-04-08 11:51:41

标签: android android-layout

我正在创建一个应用程序,其中TextInputLayout引用空指针,我无法在这里找到我的代码。

    public class MainActivity extends AppCompatActivity {

    EditText inputName,inputDepartment,inputPost;
    TextInputLayout inputLayoutName,inputLayoutDepartment,inputLayoutPost;
    Button btnSave;
    LinearLayout linearLayout;

    //DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setLogo(R.drawable.image);

        inputName=(EditText)findViewById(R.id.input_name);
        inputDepartment=(EditText)findViewById(R.id.input_dept);
        inputPost=(EditText)findViewById(R.id.input_post);

        inputLayoutName=(TextInputLayout)findViewById(R.id.input_layout_name);
        inputLayoutDepartment=(TextInputLayout)findViewById(R.id.input_layout_department);
        inputLayoutPost=(TextInputLayout)findViewById(R.id.input_layout_post);
        btnSave=(Button)findViewById(R.id.login);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitForm();
            }
        });
        if (getIntent().getBooleanExtra("EXIT", false))
        {
            finish();
        }
    }

    private void submitForm() {
        if (!validateName()) {
            return;
        }

        if (!validateDept()) {
            return;
        }

        if (!validatePost()) {
            return;
        }

        Snackbar snackbar= Snackbar.make(linearLayout,"Details Stored",Snackbar.LENGTH_LONG);
        snackbar.show();
    }

    private boolean validateName() {
        if (inputName.getText().toString().trim().isEmpty()) {
            inputLayoutName.setError(getString(R.string.err_msg_name));
            requestFocus(inputName);
            return false;
        } else {
            inputLayoutName.setErrorEnabled(false);
        }

        return true;
    }
    private boolean validateDept() {
        if (inputDepartment.getText().toString().trim().isEmpty()) {
            inputLayoutDepartment.setError(getString(R.string.err_msg_name));
            requestFocus(inputDepartment);
            return false;
        } else {
            inputLayoutDepartment.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validatePost() {
        if (inputPost.getText().toString().trim().isEmpty()) {
            inputLayoutPost.setError(getString(R.string.err_msg_name));
            requestFocus(inputPost);
            return false;
        } else {
            inputLayoutPost.setErrorEnabled(false);
        }

        return true;
    }

    private void requestFocus(View view) {
        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }


    private class MyTextWatcher implements TextWatcher{

        private View view;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            switch (view.getId()){

                case R.id.input_name:
                    validateName();
                    break;

                case R.id.input_dept:
                    validateDept();
                    break;

                case R.id.input_post:
                    validatePost();
                    break;
            }
        }
    }
}

现在在这里

  

inputLayoutName =(TextInputLayout)findViewById(R.id.input_layout_name);               inputLayoutDepartment =(TextInputLayout)findViewById(R.id.input_layout_department);               inputLayoutPost =(TextInputLayout)findViewById(R.id.input_layout_post);

引用空指针。

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ankittale.tp_contentprovider.MainActivity"
    tools:showIn="@layout/activity_main"
    android:fillViewport="true">

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="60dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/layout_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:singleLine="true"
            android:hint="@string/full_name"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/layout_dept"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_dept"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:singleLine="true"
            android:hint="@string/department"/>
    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/layout_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_post"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:singleLine="true"
            android:hint="@string/post_designation"/>
    </android.support.design.widget.TextInputLayout>

    <Space
        android:layout_width="1dp"
        android:layout_height="50dp"/>

    <LinearLayout
        android:layout_below="@+id/Password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="?android:attr/dividerVertical">
        <Button
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/save"
            style="?android:attr/buttonBarButtonStyle"/>
    </LinearLayout>


</LinearLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:1)

根据上面的XML布局代码判断,我认为你的findViewById语句应该是:

inputLayoutName = (TextInputLayout) findViewById(R.id.layout_name);
inputLayoutDepartment = (TextInputLayout) findViewById(R.id.layout_department);
inputLayoutPost = (TextInputLayout) findViewById(R.id.layout_post);

您没有包含ID的元素:输入_ layout_name,输入_ layout_department和输入_ layout_post。

干杯

答案 1 :(得分:0)

你给错了id值试试这个

inputLayoutName=(TextInputLayout)findViewById(R.id.layout_name);
inputLayoutDepartment=(TextInputLayout)findViewById(R.id.layout_dept);
inputLayoutPost=(TextInputLayout)findViewById(R.id.layout_post);