【问题标题】:Error inflating class textview in android在android中膨胀类textview时出错
【发布时间】:2017-07-22 18:39:51
【问题描述】:

这个问题是我的代码实际上可以工作,但是我设置了一些文本在编辑文本为空时显示。当我按下登录按钮时,我的应用程序崩溃了。这是错误:

android.view.InflateException:二进制 XML 文件第 17 行:二进制 XML 文件第 17 行:膨胀类 TextView 时出错 原因:android.view.InflateException: Binary XML file line #17: Error inflating class TextView 原因:java.lang.UnsupportedOperationException:无法解析索引 4 处的属性:TypedValue{t=0x2/d=0x1010099 a=1}

这是我的 xml 代码:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="56dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp">

<ImageView android:src="@drawable/logo"
    android:layout_width="wrap_content"
    android:layout_height="72dp"
    android:layout_marginBottom="24dp"
    android:layout_gravity="center_horizontal" />


<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:id="@+id/input_username_id"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:theme="@style/TextLabel"
    android:layout_marginBottom="20dp">

    <EditText
        android:id="@+id/input_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:hint="@string/hint_username"
        android:inputType="textEmailAddress"
        android:layout_marginRight="10dp"
        android:gravity="right" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_password_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="20dp"
    android:theme="@style/TextLabel">

    <EditText
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:hint="@string/hint_password"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

<android.support.v7.widget.AppCompatButton
    android:id="@+id/btn_login"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:layout_marginBottom="40dp"
    android:padding="12dp"
    android:paddingLeft="30dp"
    android:background="@drawable/my_button"
    android:drawableLeft="@drawable/ic_stat_login"
    android:textColor="@color/white"
    android:text="ورود"/>
<TextView android:id="@+id/link_signup"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="24dp"
    android:layout_marginTop="10dp"
    android:textColor="@color/white"
    android:text="create an account"
    android:gravity="center"
    android:textSize="13dp"
    />
<TextView android:id="@+id/link_forgetpass"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="24dp"
    android:layout_marginTop="10dp"
    android:textColor="@color/white"
    android:text="forget my password"
    android:gravity="center"
    android:textSize="13dp"
    />
</LinearLayout>

这是我的java代码:

_emailText = (EditText) (findViewById(R.id.input_email));
_passwordText = (EditText) (findViewById(R.id.input_password));
_loginButton = (Button) (findViewById(R.id.btn_login));

_loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
});
public void login() {
    Log.d(TAG, "Login");
    if (!validate()) {
        onLoginFailed();
        return;
    }
    email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    JsonObject json = new JsonObject();
    json.addProperty("email", email);
    json.addProperty("password", password);

    _loginButton.setEnabled(false);
    WebService.connect(LoginActivity.this, json, "login",layout_for_progressbar);
}
public boolean validate() {
    boolean valid = true;
    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
       _emailText.setError("Wrong Email");
        valid = false;
    } else {
            _emailText.setError(null);
    }
    if (password.isEmpty() || password.length() < 6 || password.length() > 15) {
        Typeface fontt = Typeface.createFromAsset(getAssets(),"fonts/ir.ttf");
        SpannableStringBuilder ssbuilder=new SpannableStringBuilder("Pass between 5 to 16");
        ssbuilder.setSpan(new CustomTypefaceSpan("",fontt),0,ssbuilder.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        _passwordText.setError(ssbuilder);
        valid = false;
    } else {
        _passwordText.setError(null);
    }
    return valid;
}

这是我的 style.xml:

 <style name="TextLabel" parent="TextAppearance.AppCompat">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/white</item>
        <item name="android:textSize">20sp</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/white</item>
        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
    </style>

【问题讨论】:

  • 展示你如何初始化两个edittext。
  • 尝试删除“android:theme="@style/TextLabel"”
  • @VivekSolanki 这是问题所在,但现在我不知道这种风格有什么问题导致应用崩溃?!
  • 我不记得确切的原因,但我遇到了同样的问题。所以,我删除了样式,而是在文本输入布局的属性字段中应用了这些属性。

标签: java android xml


【解决方案1】:

问题是我设置的样式的父级应该是

parent="Theme.AppCompat.Light.NoActionBar"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2023-03-19
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多