2016-09-27 14 views
2

いくつかの同様のスレッドが見られますが、同じものも解決策もありません。theme.appcompatテーマを使用する必要がありますが、私はそうではありませんか?

ナビゲーションビューの実装を理解するための簡単なサンプルプロジェクトをまとめようとしています。

エラーはタイトルごとです。私はappcompatテーマを使用する必要があると言われています。 しかし、私が見る限り、私は1つを使用しています!

私のスタイルファイルには、 "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); 
    } } 

のErrログ:また、お使いのベースに以下の必要

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'という新しいファイルを追加すると、ビルド時にエラーが発生します。 "無効なリソースディレクトリ"。私は現在スタジオではなくAIDEで働いています。違いがありますか? – Esby

+0

FWIW(または次の男)それは私たちの "値"サブフォーラーで、私はこれを反映するために私の答えを変更しました。 – Booger

0

それを見つけました! 私のアプリを作成する途中で、新しいフォルダが現れているようです: "res/values-v21/stles.xml ' 私の事実はわかっていませんでした。 そこに、 'AppTheme'が再び定義され、res/values/styles.xmlで作成した定義を無効にしました。それ以来

はGoogleで検索して、この上の文書を発見した。.. https://developer.android.com/training/material/compatibility.html#Theme

の提案をした人尋ねるのおかげで、私はあなたの時間を無駄にしませんでした願っています。おそらく、これが役に立つと思ういくつかの他のノブがいるでしょう!

+0

これは有効なフォルダで、v21固有のスタイル情報を格納します。それは間違っていないし、あなたのアプリはv21 +デバイスに置かれた場合でも正常に実行されます。 元の問題は、このファイルが最初から(任意のフォルダ内に)見つからなかったことでした。 – Booger

関連する問題