你需要使用theme.appcompat主题 - 但我是,不是吗?

时间:2016-09-27 23:31:06

标签: android android-appcompat

看到几个类似的线程,但没有相同或解决方案。

尝试将一个简单的示例项目放在一起,以了解navigationView的实现。

错误是按照标题,我被告知我必须使用appcompat主题。 但据我所知,我正在使用一个!?

在我的样式文件中," Theme.AppCompat.Light.NoActionBar",并在清单中引用。

我确定我错过了一些简单的东西,但作为所有这个java-android malarkey的菜鸟,我的目光还没有看到它。

非常感谢有人指出我错过了什么!!

旁注...... 对我来说,一个样式问题导致我的应用程序崩溃似乎很奇怪,不应该是样式出现'外观'不是'功能' ?

的build.gradle

apply plugin: 'com.android.application'

android {
  compileSdkVersion 21
  buildToolsVersion "21.1.0"

    defaultConfig {
      applicationId "com.mycompany.myapp2"
      minSdkVersion 14
      targetSdkVersion 21
      versionCode 1
      versionName "1.0"
  }
  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
    }
}
dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:23.1.1.aar'
  compile 'com.android.support:design:23.1.1'
}

的manifest.xml

  

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
     

RES /值/ style.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

RES /菜单/ drawer_view.xml

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_first_fragment"
        android:icon="@drawable/ic_one"
        android:title="First" />
    <item
        android:id="@+id/nav_second_fragment"
        android:icon="@drawable/ic_two"
        android:title="Second" />
    <item
        android:id="@+id/nav_third_fragment"
        android:icon="@drawable/ic_three"
        android:title="Third" />
</group>

RES /布局/ toolbar.xml

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark">

main.xml中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

main_activity.java

package com.mycompany.myapp2; 
import android.app.*; 
import android.os.*;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity; 
import android.support.design.widget.NavigationView; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v4.view.GravityCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawer;
    private Toolbar toolbar;
    private NavigationView nvDrawer;
    private ActionBarDrawerToggle drawerToggle;


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

        // Set a Toolbar to replace the ActionBar.
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Find our drawer view
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    } }

错误日志:

09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               Process: com.mycompany.myapp2, PID: 11972
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp2/com.mycompany.myapp2.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.support.design.widget.NavigationView
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:102)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:148)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5443)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.support.design.widget.NavigationView
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.Activity.setContentView(Activity.java:2170)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at com.mycompany.myapp2.MainActivity.onCreate(MainActivity.java:28)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.Activity.performCreate(Activity.java:6245)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               ... 9 more
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.NavigationView
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.createView(LayoutInflater.java:645)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               ... 17 more
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               Caused by: java.lang.reflect.InvocationTargetException
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at java.lang.reflect.Constructor.newInstance(Native Method)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.view.LayoutInflater.createView(LayoutInflater.java:619)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               ... 22 more
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
09-27 20:04:08.195 11972 11972 E   AndroidRuntime                               ... 24 more

2 个答案:

答案 0 :(得分:0)

您的基地res\values\styles.xml

还需要以下内容
<!-- Main Theme -->
    <style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar"
...
 >

答案 1 :(得分:0)

发现它! 因此,在创建我的应用程序的某个时刻,似乎出现了一个新文件夹:&#34; res / values-v21 / stles.xml&#39; 我不知道发生过这种情况,所以如果我的帖子在没有列出的情况下误导,那就道歉了。 在那里,&#39; AppTheme&#39;再次定义,推翻我在res / values / styles.xml中创建的定义。

从那时起谷歌搜索并找到了关于此的文档.. https://developer.android.com/training/material/compatibility.html#Theme

感谢问谁提出了建议,希望我没有浪费你的时间。也许会有一些其他的菜鸟会发现这很有用!