膨胀异常 - 解析TextView时出错

3

我有这个主活动。我在登录按钮上调用了validate方法。但是,当我点击按钮时,我没有收到错误消息并且我的应用程序崩溃了。任何帮助都将不胜感激。谢谢。 这是我的MainActivity Java类:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public Button blogin;
public TextView signup;
public EditText uname, pass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    uname = (EditText) findViewById(R.id.uname);
    pass = (EditText) findViewById(R.id.pass);
    signup = (TextView) findViewById(R.id.signup);
    blogin = (Button) findViewById(R.id.blogin);

    blogin.setOnClickListener(this);
    signup.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.blogin:


            validate();




            break;

        case R.id.signup:
            startActivity(new Intent(this, SignUp.class));
            break;
    }
}

@Override
protected void onPause() {
    super.onPause();
}

public boolean validate() {
    boolean valid = true;

    String email = uname.getText().toString();
    String password = pass.getText().toString();

    if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        uname.setError("Enter a valid email address");


        valid = false;
    } else {
        uname.setError(null);
    }

    if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
        pass.setError("Enter a password between 4 and 10 alphanumeric characters");


     valid = false;
    } else {
        pass.setError(null);
    }

    return valid;
}
}

这是我的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fillViewport="true">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/uname"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo" />

    <!--<requestFocus />-->

    <EditText
        android:id="@+id/uname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pass"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:drawableLeft="@drawable/ic_perm_identity_black_24dp"
        android:drawablePadding="10dp"
        android:hint="Username"
        android:inputType="textEmailAddress"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:theme="@style/TextLabel" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:drawableLeft="@drawable/ic_lock_outline_black_24dp"
        android:drawablePadding="10dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:theme="@style/TextLabel" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/blogin"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pass"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="35dp"
        android:background="@drawable/shape"
        android:fontFamily="sans-serif"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Login"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="15dp" />
    <!--style="?android:attr/borderlessButtonStyle"-->


    <TextView
        android:id="@+id/forgot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/blogin"
        android:layout_marginTop="30dp"
        android:layout_toLeftOf="@+id/textView3"
        android:layout_toStartOf="@+id/textView3"
        android:gravity="center"
        android:paddingRight="5dp"
        android:text="Forgot Password?"
        android:textColor="#000000"
        android:textSize="15dp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/signup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_toEndOf="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:text="New user? Sign up"
        android:textColor="#000000"
        android:textSize="15dp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/forgot"
        android:layout_centerHorizontal="true"
        android:text="|"
        android:textColor="#000000"
        android:textSize="15dp" />


</RelativeLayout>
</ScrollView>
    <style name="TextLabel" parent="TextAppearance.AppCompat">
    <item name="android:textColorHint">@color/HintColor</item>
    <item name="android:textSize">15sp</item>
    <item name="colorAccent">@color/editTextLine</item>
    <item name="colorControlNormal">@color/editTextLine</item>
    <item name="colorControlActivated">@color/editTextActivated</item>
    <item name="android:textColor">@color/textColor</item>
</style>

我遇到了以下错误- android.view.InflateException: 二进制XML文件第17行:二进制XML文件第17行:TextView类充气错误


1
我已经测试了您的XML,没有使用 **android:theme="@style/TextLabel"**,它可以正常工作。 - Alexander
@Alexander 谢谢。我尝试了一下,是的,它可以在没有 android:theme="@style/TextLabel" 的情况下工作。但是为什么使用这个主题就不行呢? - Namrata Singh
@Kenumir 不是这样的。我按照你说的做了,但问题仍然存在。 - Namrata Singh
@NamrataSingh,风格也都没问题。我认为问题出在你的某些颜色上。 - Alexander
@Alexander 问题已经解决。顺便说一下,谢谢 :) - Namrata Singh
显示剩余4条评论
1个回答

9
问题出在TextLabel样式的父主题上。我将其更改为parent="AppTheme.NoActionBar"。现在它可以正常工作了。

这真的修复了我的崩溃。谢谢。 - Alexandro
我也是!谢谢分享! - Amos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接