我对Android编码很新,所以请耐心等待。我一直在关注这个很棒的教程,我已经关注了所有内容,但我现在遇到了问题。每当我点击"注册"时,应用程序崩溃。当我构建我的应用程序时,它工作得很好。有人能找到解决方案吗?
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.espina.espinasis, PID: 5040
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at android.support.design.widget.Snackbar.make(Snackbar.java:137)
at com.example.espina.espinasis.activities.RegisterActivity.postDataToSQLite(RegisterActivity.java:186)
at com.example.espina.espinasis.activities.RegisterActivity.onClick(RegisterActivity.java:117)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
RegisterActivity.java
package com.example.espina.espinasis.activities;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import com.example.espina.espinasis.R;
import com.example.espina.espinasis.helper.InputValidation;
import com.example.espina.espinasis.model.User;
import com.example.espina.espinasis.sql.DatabaseHelper;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private final AppCompatActivity activity = RegisterActivity.this;
private NestedScrollView nestedScrollView;
private TextInputLayout textInputLayoutStudnum;
private TextInputLayout textInputLayoutLastname;
private TextInputLayout textInputLayoutFirstname;
private TextInputLayout textInputLayoutAge;
private TextInputLayout textInputLayoutSex;
private TextInputLayout textInputLayoutCourse;
private TextInputLayout textInputLayoutYearlevel;
private TextInputLayout textInputLayoutEmail;
private TextInputLayout textInputLayoutUsername;
private TextInputLayout textInputLayoutPassword;
private TextInputLayout textInputLayoutConfirmPassword;
private TextInputEditText textInputEditTextStudnum;
private TextInputEditText textInputEditTextLastname;
private TextInputEditText textInputEditTextFirstname;
private TextInputEditText textInputEditTextAge;
private TextInputEditText textInputEditTextSex;
private TextInputEditText textInputEditTextCourse;
private TextInputEditText textInputEditTextYearlevel;
private TextInputEditText textInputEditTextEmail;
private TextInputEditText textInputEditTextUsername;
private TextInputEditText textInputEditTextPassword;
private TextInputEditText textInputEditTextConfirmPassword;
private AppCompatButton appCompatButtonRegister;
private AppCompatTextView appCompatTextViewLoginLink;
private InputValidation inputValidation;
private DatabaseHelper databaseHelper;
private User user;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().hide();
initViews();
initListeners();
initObjects();
}
private void initViews(){
nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
textInputLayoutStudnum = (TextInputLayout) findViewById(R.id.textInputLayoutStudnum);
textInputLayoutLastname= (TextInputLayout) findViewById(R.id.textInputLayoutLastname);
textInputLayoutFirstname = (TextInputLayout) findViewById(R.id.textInputLayoutFirstname);
textInputLayoutAge = (TextInputLayout) findViewById(R.id.textInputLayoutAge);
textInputLayoutSex = (TextInputLayout) findViewById(R.id.textInputLayoutSex);
textInputLayoutCourse = (TextInputLayout) findViewById(R.id.textInputLayoutCourse);
textInputLayoutYearlevel = (TextInputLayout) findViewById(R.id.textInputLayoutYearlevel);
textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
textInputLayoutUsername = (TextInputLayout) findViewById(R.id.textInputLayoutUsername);
textInputLayoutPassword= (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
textInputLayoutConfirmPassword = (TextInputLayout) findViewById(R.id.textInputLayoutConfirmpassword);
textInputEditTextStudnum = (TextInputEditText) findViewById(R.id.textInputEditTextStudnum);
textInputEditTextLastname = (TextInputEditText) findViewById(R.id.textInputEditTextLastname);
textInputEditTextFirstname = (TextInputEditText) findViewById(R.id.textInputEditTextFirstname);
textInputEditTextAge = (TextInputEditText) findViewById(R.id.textInputEditTextAge);
textInputEditTextSex = (TextInputEditText) findViewById(R.id.textInputEditTextSex);
textInputEditTextCourse = (TextInputEditText) findViewById(R.id.textInputEditTextCourse);
textInputEditTextYearlevel = (TextInputEditText) findViewById(R.id.textInputEditTextYearlevel);
textInputEditTextEmail = (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
textInputEditTextUsername = (TextInputEditText) findViewById(R.id.textInputEditTextUsername);
textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);
textInputEditTextConfirmPassword = (TextInputEditText) findViewById(R.id.textInputEditTextConmfirmpassword);
appCompatButtonRegister = (AppCompatButton) findViewById(R.id.appCompatButtonRegister);
appCompatTextViewLoginLink = (AppCompatTextView) findViewById(R.id.appCompatTextViewLoginLink);
}
private void initListeners(){
appCompatButtonRegister.setOnClickListener(this);
appCompatTextViewLoginLink.setOnClickListener(this);
}
private void initObjects(){
inputValidation = new InputValidation(activity);
databaseHelper = new DatabaseHelper(activity);
user = new User();
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.appCompatButtonRegister:
postDataToSQLite();
break;
case R.id.appCompatTextViewLoginLink:
finish();
break;
}
}
private void postDataToSQLite(){
if (!inputValidation.isInputEditTextFilled(textInputEditTextStudnum, textInputLayoutStudnum, getString(R.string.error_message_studnum))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextLastname, textInputLayoutLastname, getString(R.string.error_message_lastname))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextFirstname, textInputLayoutFirstname, getString(R.string.error_message_firstname))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextAge, textInputLayoutAge, getString(R.string.error_message_age))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextSex, textInputLayoutSex, getString(R.string.error_message_sex))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextCourse, textInputLayoutCourse, getString(R.string.error_message_course))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextYearlevel, textInputLayoutYearlevel, getString(R.string.error_message_yearlevel))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (!inputValidation.isInputTextEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextUsername, textInputLayoutUsername, getString(R.string.error_message_username))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_password))) {
return;
}
if (!inputValidation.isInputEditTextMatches(textInputEditTextPassword, textInputEditTextConfirmPassword,
textInputLayoutConfirmPassword, getString(R.string.error_password_match))) {
return;
}
if (!databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim())) {
user.setStudnum(textInputEditTextStudnum.getText().toString().trim());
user.setLastname(textInputEditTextLastname.getText().toString().trim());
user.setFirstname(textInputEditTextFirstname.getText().toString().trim());
user.setAge(textInputEditTextAge.getText().toString().trim());
user.setSex(textInputEditTextSex.getText().toString().trim());
user.setCourse(textInputEditTextCourse.getText().toString().trim());
user.setYearlevel(textInputEditTextYearlevel.getText().toString().trim());
user.setEmail(textInputEditTextEmail.getText().toString().trim());
user.setUsername(textInputEditTextUsername.getText().toString().trim());
user.setPassword(textInputEditTextPassword.getText().toString().trim());
databaseHelper.addUser(user);
// Snack Bar to show success message that record saved successfully
Snackbar.make(nestedScrollView, getString(R.string.success_message), Snackbar.LENGTH_LONG).show();
emptyInputEditText();
} else {
// Snack Bar to show error message that record already exists
Snackbar.make(nestedScrollView, getString(R.string.error_email_exists), Snackbar.LENGTH_LONG).show();
}
}
private void emptyInputEditText(){
textInputEditTextStudnum.setText(null);
textInputEditTextLastname.setText(null);
textInputEditTextFirstname.setText(null);
textInputEditTextAge.setText(null);
textInputEditTextSex.setText(null);
textInputEditTextCourse.setText(null);
textInputEditTextYearlevel.setText(null);
textInputEditTextEmail.setText(null);
textInputEditTextUsername.setText(null);
textInputEditTextPassword.setText(null);
textInputEditTextConfirmPassword.setText(null);
}
activity_register_activity.xml
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutStudnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextStudnum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_studnum"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutLastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextLastname"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_lastname"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutFirstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextFirstname"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_firstname"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextAge"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_age"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_sex"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextCourse"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_course"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutYearlevel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextYearlevel"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_yearlevel"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextEmail"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_email"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextUsername"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_username"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextPassword"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_password"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutConfirmpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextConmfirmpassword"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_confirm_password"
android:inputType="text"
android:maxLines="1"
android:textColor="@color/colorText"
android:textColorHint="@color/colorText" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/appCompatButtonRegister"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:textColor="@color/colorText"
android:background="@color/colorTextHint"
android:text="@string/text_register"></android.support.v7.widget.AppCompatButton>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/appCompatTextViewLoginLink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:textAlignment="center"
android:text="@string/text_already_member"
android:textSize="16sp"
android:textColor="@color/colorText"/>
</android.support.v7.widget.LinearLayoutCompat>