在 Android Studio 中扩充 TextView class 时出错

Error inflating TextView class in Android Studio

我是 Android SDK 和 Android Studio 的新手,但我熟悉编程。我有两个应该链接在一起的活动,但无论出于何种原因,由于我的 XML 文件之一与我创建的 TextView 对象有关的错误,我的应用程序在启动时崩溃。我不知道那里发生了什么。感谢您的帮助!

我 90% 确定这个文件是罪魁祸首。如果您认为有必要,我可以根据要求上传更多代码。下面是错误。

<?xml version="1.0" encoding="utf-8"?>
<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"
    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.txfbla.benjamin.fblattire.LoginActivity">

    <TextView android:id="@+id/login_usernameHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:textSize="18sp"
        android:textColor="@string/login_blue"/>

    <EditText android:id="@+id/login_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textAutoComplete" />

    <TextView android:id="@+id/login_passwordHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        android:textSize="18sp"
        android:textColor="@string/login_blue"/>

    <EditText android:id="@+id/login_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>

    <Button android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="16sp"
        android:onClick="logIn"/>

</LinearLayout>

package com.txfbla.benjamin.fblattire;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity {

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

public void logIn(View view){
    Intent intent = new Intent(this, HomePageActivity.class);
    EditText editText = (EditText) findViewById(R.id.login_username);
    String username = editText.getText().toString();
    intent.putExtra("username", username);
    startActivity(intent);
}
}

这是我要加载的activity:

package com.txfbla.benjamin.fblattire;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class HomePageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action",     Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}

这是我遇到的错误:

01-09 02:16:15.144 25368-25368/com.txfbla.benjamin.fblattire E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.txfbla.benjamin.fblattire, PID: 25368
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txfbla.benjamin.fblattire/com.txfbla.benjamin.fblattire.LoginActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                               at android.app.ActivityThread.access0(ActivityThread.java:144)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                            Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                               at com.txfbla.benjamin.fblattire.LoginActivity.onCreate(LoginActivity.java:14)
                                                                               at android.app.Activity.performCreate(Activity.java:5933)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                               at android.app.ActivityThread.access0(ActivityThread.java:144) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:135) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                                                                            Caused by: android.content.res.Resources$NotFoundException: File #3677A1 from drawable resource ID #0x7f060018: .xml extension required
                                                                               at android.content.res.Resources.loadColorStateList(Resources.java:2549)
                                                                               at android.content.res.TypedArray.getColorStateList(TypedArray.java:427)
                                                                               at android.widget.TextView.<init>(TextView.java:987)
                                                                               at android.widget.TextView.<init>(TextView.java:629)
                                                                               at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60)
                                                                               at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:56)
                                                                               at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:92)
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
                                                                               at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                               at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                               at com.txfbla.benjamin.fblattire.LoginActivity.onCreate(LoginActivity.java:14) 
                                                                               at android.app.Activity.performCreate(Activity.java:5933) 
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                               at android.app.ActivityThread.access0(ActivityThread.java:144) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:135) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

您不能从字符串资源设置 textColor。您可以像#FFFFFF 一样对值进行硬编码,也可以将值保存在 colors.xml 文件

colors.xml

<color name="white">#FFFFFF</color>

改变

<TextView android:id="@+id/login_usernameHeading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/username"
    android:textSize="18sp"
    android:textColor="@string/login_blue "/>

<TextView android:id="@+id/login_usernameHeading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/username"
    android:textSize="18sp"
    android:textColor="@color/white" />

同样将第二个 TextView textColor 更改为颜色资源。