当电子邮件和密码字段为空时,单击注册会使应用程序崩溃,但是当名称字段为空时,它可以正常工作,这似乎是问题所在?
xml文件:
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
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.zachmarcelo.softcash.LoginActivity"
android:background="#fff"
>
<!-- Login progress -->
<ImageView
android:layout_width="match_parent"
android:layout_height="47dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
app:srcCompat="@drawable/icon_ewallet_" />
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColorHint="@color/colorHint" >
<EditText
android:id="@+id/reg_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="text"
android:textSize="20sp"
android:textColor="@android:color/background_dark"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel"
android:textColorHint="@color/colorHint">
<EditText
android:id="@+id/reg_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/darker_gray"
android:hint="Email"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/background_dark"
android:inputType="textEmailAddress"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel"
android:textColorHint="@color/colorHint">
<EditText
android:id="@+id/reg_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/background_dark"
android:hint="Password"
android:imeActionId="6"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/background_dark" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/register"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorAccent"
android:gravity="center"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:text="Register"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/signin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Already have an account?"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="12dp" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
代码:
package com.example.zachmarcelo.softcash;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
public class RegisterActivity extends AppCompatActivity implements
View.OnClickListener {
private EditText reg_password,
reg_username,
reg_email;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
progressBar = new ProgressBar(getApplicationContext());
reg_password = findViewById(R.id.reg_password);
reg_email = findViewById(R.id.reg_email);
reg_username = findViewById(R.id.reg_username);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.register).setOnClickListener(this);
findViewById(R.id.signin).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
Intent i = new Intent(RegisterActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
}
private void registerUser() {
final String username = reg_username.getText().toString().trim();
final String email = reg_email.getText().toString().trim();
String password = reg_password.getText().toString().trim();
if (username.equals("")) {
reg_username.setError(getString(R.string.input_error_name));
reg_username.requestFocus();
return;
}
if (email.equals("")) {
reg_email.setError(getString(R.string.input_error_email));
reg_email.requestFocus();
return;
}else{
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
reg_email.setError(getString(R.string.input_error_email_invalid));
reg_email.requestFocus();
return;
}
}
if (password.equals("")) {
reg_password.setError(getString(R.string.input_error_password));
reg_password.requestFocus();
return;
}else{
if (password.length() < 8) {
reg_password.setError(getString(R.string.input_error_password_length));
reg_password.requestFocus();
return;
}
}
progressBar.setVisibility(View.VISIBLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
User user = new User(
username,
email
);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, getString(R.string.registration_success), Toast.LENGTH_LONG).show();
Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(RegisterActivity.this, getString(R.string.registration_failed), Toast.LENGTH_LONG).show();
}
}
});
} else {
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.register:
registerUser();
break;
case R.id.signin:
Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
finish();
break;
}
}
}
错误日志:
09-03 13:25:24.942 15875-15875/com.example.zachmarcelo.softcash E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zachmarcelo.softcash, PID: 15875
android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:492)
at android.widget.TextView.<init>(TextView.java:955)
at android.widget.TextView.<init>(TextView.java:872)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:75)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.widget.Editor.showError(Editor.java:505)
at android.widget.Editor.onFocusChanged(Editor.java:1237)
at android.widget.TextView.onFocusChanged(TextView.java:9821)
at android.view.View.handleFocusGainInternal(View.java:6631)
at android.view.View.requestFocusNoSearch(View.java:10890)
at android.view.View.requestFocus(View.java:10869)
at android.view.View.requestFocus(View.java:10836)
at android.view.View.requestFocus(View.java:10778)
at com.example.zachmarcelo.softcash.RegisterActivity.registerUser(RegisterActivity.java:74)
at com.example.zachmarcelo.softcash.RegisterActivity.onClick(RegisterActivity.java:143)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
这就是应该发生的情况:(在名称文本中完美运行,但在其他Edittext中却不行)
任何帮助将不胜感激:D
答案 0 :(得分:1)
我的猜测是,它与您放入@style/TextLabel
中的内容有关。
如果密码或电子邮件有误,您将以编程方式请求关注。很好,但是当这种情况发生时,您的样式中的某些内容正在寻找不存在的颜色资源。这就是导致错误的原因。
答案 1 :(得分:0)
错误似乎与textview的getColor()方法有关,您的原色有问题,请检查一下。