2016-09-27 69 views
2

看到幾個類似的線程,但沒有相同或有解決方案。您需要使用theme.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 
+0

您是否收到任何錯誤? –

+0

你已經包含了一切,但最重要的部分:你聲明你的''的清單以及它使用的是什麼主題。 – ianhanniballake

+0

在styles.xml中也需要它 – Booger

回答

0

你還需要在你的基地以下res\values\styles.xml

<!-- Main Theme --> 
    <style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar" 
... 
> 
+0

如果我在'res'下添加一個新文件'styles.xml',那麼在構建時會出現aapt錯誤。 「資源目錄無效」。我目前在AIDE而不是Studio工作。這會有所作爲嗎? – Esby

+0

FWIW(或下一個傢伙)在「價值」子元素中,我改變了我的答案以反映這一點。 – Booger

0

發現! 因此,在創建我的應用程序的過程中,似乎出現了一個新的文件夾:「res/values-v21/stles.xml' 我沒有意識到發生了這種情況,所以如果我的文章誤導了未列出該文件,那麼appologise。 在那裏,'AppTheme'被再次定義,否定了我在res/values/styles.xml中創建的定義。

從那以後用Google搜索,發現文件上這個.. https://developer.android.com/training/material/compatibility.html#Theme

感謝請問誰提出的建議,希望我沒有浪費你的時間。也許會有其他的noob誰會發現這個有用的!

+0

這是一個有效的文件夾,您可以在其中放置特定於v21的樣式信息。這是不正確的,如果將它放在v21 +設備上,您的應用程序甚至可以正常運行。 你原來的問題是,你最初(在任何文件夾)缺少這個文件。 – Booger

相關問題