无操作栏主题的渲染问题.找不到以下类:android.support.v7.widget.AppCompatTextVi



我在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>

这是我的应用程序成绩:

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,使缓存无效。但是,这些都不起作用。主题可能有问题,但我想使用"无操作栏"主题。这是我的风格.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作为父主题。

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

<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>

显然,问题出在主题上。在我尝试将其更改为:

<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style

这是带有禁用操作栏选项的新主题。成功了。我在如何设置appcompat-v7工具栏下找到了这个,比如Theme.AppCompat.Light.DarkActionBar?

相关内容

最新更新