java - NoActionBar 主题的渲染问题。找不到以下类: android. support.v7.widget.AppCompatTextView

标签 java android

我在 Android Studio 中的项目正在运行和执行,但是每当我单击 Activity_login.xml 时,我看不到任何元素,而只是一个基本布局,我看不到文本输入,甚至看不到按钮。

这是我预览布局时遇到的错误:

Rendering Problems The following classes could not be instantiated:
- android.support.v7.widget.AppCompatTextView 

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.example.myapp.mealplanner.LoginActivity">

<include
    android:id="@+id/login_toolbar"
    layout="@layout/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:paddingEnd="16dp"
    android:paddingStart="16dp"
    android:text="@string/login_statement"
    android:textColor="@color/black"
    android:textSize="18sp"
    android:textStyle="bold" />


<android.support.design.widget.TextInputLayout
    android:id="@+id/login_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:hint="@string/enter_your_email"
        android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/login_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:hint="@string/enter_your_password"
        android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>


<Button
    android:id="@+id/login_login_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:text="@string/login" />

</LinearLayout>

这是我的应用程序 gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
    applicationId "com.example.myapp.mealplanner"
    minSdkVersion 19
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
productFlavors {
    }
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
{
    exclude group: 'com.android.support', module: 'support-annotations'
})
//compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:appcompat-v7:26.+'
//compile 'com.android.support:design:26.+'
compile 'com.android.support:design:26.1.0'
compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-auth:11.4.2'
compile 'com.google.firebase:firebase-storage:11.4.2'
compile 'com.firebaseui:firebase-ui-database:3.0.0'
compile 'com.firebaseui:firebase-ui-auth:3.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
//compile 'com.android.support:recyclerview-v7:26.1.0'
//compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.android.support:cardview-v7:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services:11.4.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.android.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
}
apply plugin: 'com.google.gms.google-services'

我猜问题可能来自 Gradle,我尝试更新我的 Android Studio,使缓存失效。然而,这些都不起作用。主题可能有问题,但我想使用“无操作栏”主题。这是我的 style.xml 文件:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

如何解决这个问题?

最佳答案

这是 appcompat v26 中的一个小错误。

在您的styles.xml中,使用Base.Theme.AppCompat而不是Theme.AppCompat作为父主题。

显然 Base 中的 NoActionBar 没有这样的主题。因此请使用如下主题:

<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

关于java - NoActionBar 主题的渲染问题。找不到以下类: android. support.v7.widget.AppCompatTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801935/

相关文章:

Java路径: Build and absolute path

Android Studio : link to external module?

java - 单击按钮更新 ListView 行项目

java - 空列表 : ArrayAdapter in Fragment

android - Flutter/Dart Android应用程式NoSuchMethodError

android - adb shell mount -o remount,rw -t yaffs2/dev/block/mtdblock0/system 不允许操作

android - openCV 单应输出掩码值用作透视误差的内点?

java - 更改一个 ViewHolder 项目也会影响其他项目

java - Autowiring 在 junit spring mvc 5 中不起作用

javascript - Node.js 应用程序的配置管理选项