getSupportActionBar使用报错

import android.app.ActionBar;

ActionBar actionBar = getSupportActionBar();

android studio界面显示:
incompatible type
Required: android.app.ActionBar
Found: android.support.v7.app.ActionBar

但是将android.support.v7.app.ActionBar改为android.app.ActionBar;仍然有问题,
编译报错为:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task
‘:app:compileDebugJavaWithJavac’. at
org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at
org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at
org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
at
org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
at
org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at
org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
at
org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
at
org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
at
org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
at
org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at
org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
at
org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at
org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
at
org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker 1. r u n ( D e f a u l t T a s k G r a p h E x e c u t e r . j a v a : 248 ) a t o r g . g r a d l e . i n t e r n a l . p r o g r e s s . D e f a u l t B u i l d O p e r a t i o n E x e c u t o r 1.run(DefaultTaskGraphExecuter.java:248) at org.gradle.internal.progress.DefaultBuildOperationExecutor 1.run(DefaultTaskGraphExecuter.java:248)atorg.gradle.internal.progress.DefaultBuildOperationExecutorRunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at
org.gradle.internal.progress.DefaultBuildOperationExecutor R u n n a b l e B u i l d O p e r a t i o n W o r k e r . e x e c u t e ( D e f a u l t B u i l d O p e r a t i o n E x e c u t o r . j a v a : 328 ) a t o r g . g r a d l e . i n t e r n a l . p r o g r e s s . D e f a u l t B u i l d O p e r a t i o n E x e c u t o r . e x e c u t e ( D e f a u l t B u i l d O p e r a t i o n E x e c u t o r . j a v a : 199 ) a t o r g . g r a d l e . i n t e r n a l . p r o g r e s s . D e f a u l t B u i l d O p e r a t i o n E x e c u t o r . r u n ( D e f a u l t B u i l d O p e r a t i o n E x e c u t o r . j a v a : 110 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k G r a p h E x e c u t e r RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)atorg.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)atorg.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)atorg.gradle.execution.taskgraph.DefaultTaskGraphExecuterEventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
at
org.gradle.execution.taskgraph.DefaultTaskGraphExecuter E v e n t F i r i n g T a s k W o r k e r . e x e c u t e ( D e f a u l t T a s k G r a p h E x e c u t e r . j a v a : 230 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k P l a n E x e c u t o r EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)atorg.gradle.execution.taskgraph.DefaultTaskPlanExecutorTaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
at
org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access 200 ( D e f a u l t T a s k P l a n E x e c u t o r . j a v a : 79 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k P l a n E x e c u t o r 200(DefaultTaskPlanExecutor.java:79) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor 200(DefaultTaskPlanExecutor.java:79)atorg.gradle.execution.taskgraph.DefaultTaskPlanExecutorTaskExecutorWorker 1. e x e c u t e ( D e f a u l t T a s k P l a n E x e c u t o r . j a v a : 104 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k P l a n E x e c u t o r 1.execute(DefaultTaskPlanExecutor.java:104) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor 1.execute(DefaultTaskPlanExecutor.java:104)atorg.gradle.execution.taskgraph.DefaultTaskPlanExecutorTaskExecutorWorker 1. e x e c u t e ( D e f a u l t T a s k P l a n E x e c u t o r . j a v a : 98 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k E x e c u t i o n P l a n . e x e c u t e ( D e f a u l t T a s k E x e c u t i o n P l a n . j a v a : 626 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k E x e c u t i o n P l a n . e x e c u t e W i t h T a s k ( D e f a u l t T a s k E x e c u t i o n P l a n . j a v a : 581 ) a t o r g . g r a d l e . e x e c u t i o n . t a s k g r a p h . D e f a u l t T a s k P l a n E x e c u t o r 1.execute(DefaultTaskPlanExecutor.java:98) at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626) at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor 1.execute(DefaultTaskPlanExecutor.java:98)atorg.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)atorg.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)atorg.gradle.execution.taskgraph.DefaultTaskPlanExecutorTaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
at
org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at
org.gradle.internal.concurrent.ManagedExecutorImpl 1. r u n ( M a n a g e d E x e c u t o r I m p l . j a v a : 46 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( T h r e a d P o o l E x e c u t o r . j a v a : 1142 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r 1.run(ManagedExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor 1.run(ManagedExecutorImpl.java:46)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:617)
at
org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:745) Caused by:
org.gradle.api.internal.tasks.compile.CompilationFailedException:
Compilation failed; see the compiler error output for details. at
org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:50)
at
org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:35)
at
org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:98)
at
org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:51)
at
org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:37)
at
org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:35)
at
org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25)
at
org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:39)
at
org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:24)
at
org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:207)
at
org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:133)
at
com.android.build.gradle.tasks.factory.AndroidJavaCompile.compile(AndroidJavaCompile.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) at
org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
at
org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
at
org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
at
org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter 1. r u n ( E x e c u t e A c t i o n s T a s k E x e c u t e r . j a v a : 121 ) a t o r g . g r a d l e . i n t e r n a l . p r o g r e s s . D e f a u l t B u i l d O p e r a t i o n E x e c u t o r 1.run(ExecuteActionsTaskExecuter.java:121) at org.gradle.internal.progress.DefaultBuildOperationExecutor 1.run(ExecuteActionsTaskExecuter.java:121)atorg.gradle.internal.progress.DefaultBuildOperationExecutorRunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
at
org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
at
org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
at
org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
at
org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
at
org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
… 32 more

奇怪的是:
将直接打印就不会报错: Log.d(TAG, "onCreate: actionbar : " + getSupportActionBar());

按照https://www.cnblogs.com/woaixingxing/p/7754873.html 查找原因
(本地使用./gradlew compileDebug --stacktrace ),也没有查找处理准确的原因

而且改为:ActionBar actionBar = getActionBar();也没有问题

最后对比,找到原因了:
1)因为我最先使用的getActionBar()方法,该方法返回的是Activity类里的方法,返回的是android.app.ActionBar对象
2)发现getActionBar()方法返回的是null,所以使用的了getSupportActionBar()方法,该方法是android.support.v7.app.AppCompatActivity类里的方法,返回的对象是android.support.v7.app.ActionBar类型

所以将import android.app.ActionBar改为import android.support.v7.app.AppCompatActivity就可以了
回到最开始Android studio显示的:
incompatible type
Required: android.app.ActionBar
Found: android.support.v7.app.ActionBar

应该就是赋值左边是android.app.ActionBar(required),右边是android.support.v7.app.ActionBar,不兼容的意思.

你可能感兴趣的:(Android)