2015-05-30 109 views
1

我正在嘗試在我的應用中使用工具欄。過去我沒有遇到任何問題。在我嘗試使用此工具欄上的佈局,我得到的錯誤:無法在Android Studio中導入android.support.v7.appcompat.R

java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$attr 
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413) 
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105) 
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:177) 
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:214) 
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:142) 
at android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:117) 
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:858) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:385) 
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:413) 
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:321) 
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350) 
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:497) 
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:485) 
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:894) 
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:485) 
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:590) 
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480) 
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320) 
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310) 
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254) 
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269) 
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227) 
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217) 
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238) 
at com.intellij.util.Alarm$Request$1.run(Alarm.java:351) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745) 

在「Toolbar.java」,該import android.support.v7.appcompat.R;語句失敗 - 它說cannot resolve symbol 'R'。我已經嘗試重建項目,我有我的build.gradle文件中正確的'編譯'依賴項,我已經嘗試'文件>無效緩存/重新啓動'我不知道什麼問題是。這是我的build.gradle文件:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 22 
    buildToolsVersion "22.0.1" 

    defaultConfig { 
     applicationId "me.bausware.set" 
     minSdkVersion 15 
     targetSdkVersion 22 
     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:22.2.0' 
} 

回答

0

檢查您在styles.xml中設置的主題。當你正在使用的新程序兼容性Toolbar那麼你應該改變你的主題,從程序兼容性主題的一個繼承,例如:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="android:colorPrimary">@color/primary</item> 
     <item name="android:colorPrimaryDark">@color/primary_dark</item> 
     <item name="android:colorAccent">@color/accent</item> 
    </style> 
</resources> 

你可以閱讀更多有關使用程序兼容性ToolbarAndroid Developers Blog

相關問題