我有一个带有注册片段的应用,用户可以自行注册。
当我开始在任何EditText上输入时,该应用程序只是异常崩溃。
错误记录
01-20 22:52:40.942 6164-6164/in.snotes.snotes E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.snotes.snotes, PID: 6164
java.lang.IndexOutOfBoundsException
at android.graphics.Canvas.drawText(Canvas.java:1707)
at android.text.Layout.drawText(Layout.java:396)
at android.widget.Editor.drawHardwareAccelerated(Editor.java:1735)
at android.widget.Editor.onDraw(Editor.java:1657)
at android.widget.TextView.onDraw(TextView.java:5888)
at android.view.View.draw(View.java:17086)
at android.view.View.updateDisplayListIfDirty(View.java:16068)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3752)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3732)
at android.view.View.updateDisplayListIfDirty(View.java:16031)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:657)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:663)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:771)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2808)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2616)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2223)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
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:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
这是我的寄存器片段xml
<LinearLayout 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"
android:orientation="vertical"
tools:context="in.snotes.snotes.ui.RegisterFragment">
<android.support.v7.widget.Toolbar
android:id="@+id/register_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="@string/register_snotes"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.design.widget.TextInputLayout
android:id="@+id/name_input_layout"
style="@style/InputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:textAllCaps="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/email_input_layout"
style="@style/InputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/password_input_layout"
style="@style/InputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/confirm_password_input_label"
style="@style/InputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/edt_confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/confirm_password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:background="@color/colorAccent"
android:text="@string/register" />
这是我的java类
public class RegisterFragment extends Fragment {
@BindView(R.id.register_toolbar)
Toolbar registerToolbar;
@BindView(R.id.edt_name)
EditText edtName;
@BindView(R.id.name_input_layout)
TextInputLayout nameInputLayout;
@BindView(R.id.edt_email)
EditText edtEmail;
@BindView(R.id.email_input_layout)
TextInputLayout emailInputLayout;
@BindView(R.id.edt_password)
EditText edtPassword;
@BindView(R.id.password_input_layout)
TextInputLayout passwordInputLayout;
@BindView(R.id.edt_confirm_password)
EditText edtConfirmPassword;
@BindView(R.id.confirm_password_input_label)
TextInputLayout confirmPasswordInputLabel;
@BindView(R.id.btn_register)
Button btnRegister;
Unbinder unbinder;
private RegisterFragmentListener mListener;
public RegisterFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static RegisterFragment newInstance() {
return new RegisterFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_register, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof RegisterFragmentListener) {
mListener = (RegisterFragmentListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement RegisterFragmentListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@OnClick(R.id.btn_register)
public void registerClicked() {
invalidateErrorMessages();
String name = edtName.getText().toString().trim();
if (name.isEmpty() || TextUtils.isEmpty(name)){
nameInputLayout.setError("Name cannot be empty");
return;
}
String email = edtEmail.getText().toString().trim();
if (email.isEmpty() || TextUtils.isEmpty(email)){
emailInputLayout.setError("Email cannot be empty");
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
emailInputLayout.setError("Enter a valid email");
return;
}
String password = edtPassword.getText().toString().trim();
String confirmPassword = edtConfirmPassword.getText().toString().trim();
if (password.isEmpty() || TextUtils.isEmpty(password)){
passwordInputLayout.setError("Password cannot be empty");
return;
}
if (confirmPassword.isEmpty() || TextUtils.isEmpty(confirmPassword)){
confirmPasswordInputLabel.setError("Confirm Password cannot be empty");
return;
}
if (!Objects.equals(password,confirmPassword)){
passwordInputLayout.setError("Passwords should match");
confirmPasswordInputLabel.setError("Passwords should match");
return;
}
mListener.register(name,email,password);
}
private void invalidateErrorMessages() {
nameInputLayout.setError(null);
emailInputLayout.setError(null);
passwordInputLayout.setError(null);
confirmPasswordInputLabel.setError(null);
}
public interface RegisterFragmentListener {
void register(String name, String email, String password);
}}
我不知道为什么当我开始输入带有Index Out Of Bounds异常的应用程序时应用程序崩溃。
什么错了?我该如何解决?
答案 0 :(得分:1)
从android:textAllCaps="true"
EditText
媒体资源
textAllCaps
属性仅适用于TextView
。您不能将此属性用于EditText
。
对于EditText
个上限,请使用此代码
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});