2016-04-08 10 views
1

Ich erstelle eine Anwendung, in der TextInputLayout auf Null-Zeiger verweist und ich bin nicht in der Lage, hier ist mein Code.TextInputLayout ist Refrencing auf Null-Objekt?

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

Hier

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

verweist auf Nullzeiger.

<?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> 

Antwort

1

Gemessen an der XML-Layout-Code oben, ich glaube, Ihre findViewById Aussagen sein sollte:

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

Sie haben noch Elemente mit IDs: input_ layout_name, input_ layout_department und input_ layout_post.

Cheers

0

Sie gegeben haben, falsch Wert id versuchen, diese

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