我正在尝试为这样的东西创建一个构造函数。
public <T extends AppCompatActivity & DialogInterface.OnDismissListener> GooglePlayUtils(T activity) {
this.activity = activity;
}
Android工作室对此没有任何抱怨,但CompileDebugJava上的构建失败
但是如果删除第二个边界,编译工作正常。 我做错了还是还有其他因素导致我失踪?
修改 将类型参数添加到类本身后而不是构造函数为我解决了问题。但是我仍然保持这个问题是开放的,所以有人可以解释为什么这样做?
编辑2 将类型参数添加到类而不是构造函数
public abstract class GooglePlayUtils<T extends AppCompatActivity & DialogInterface.OnDismissListener> {
private T activity;
public GooglePlayUtils(T activity) {
this.activity = activity;
}
错误
java.lang.AssertionError: at com.sun.tools.javac.util.Assert.error(Assert.java:133) 在com.sun.tools.javac.util.Assert.check(Assert.java:86) at com.sun.tools.javac.comp.Attr.attribBounds(Attr.java:561) at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:723) at com.sun.tools.javac.tree.JCTree $ JCMethodDecl.accept(JCTree.java:669) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:480) at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:3250) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:3173) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:3109) at com.sun.tools.javac.comp.Attr.visitClassDef(Attr.java:708) at com.sun.tools.javac.tree.JCTree $ JCClassDecl.accept(JCTree.java:591) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:480) at com.sun.tools.javac.comp.Attr.visitNewClass(Attr.java:1790) at com.sun.tools.javac.tree.JCTree $ JCNewClass.accept(JCTree.java:1372) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:449) at com.sun.tools.javac.comp.Attr.visitAssign(Attr.java:1993) at com.sun.tools.javac.tree.JCTree $ JCAssign.accept(JCTree.java:1466) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:460) at com.sun.tools.javac.comp.Attr.visitExec(Attr.java:1294) at com.sun.tools.javac.tree.JCTree $ JCExpressionStatement.accept(JCTree.java:1167) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:480) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:496) at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:918) at com.sun.tools.javac.tree.JCTree $ JCBlock.accept(JCTree.java:781) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:480) at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:836) at com.sun.tools.javac.tree.JCTree $ JCMethodDecl.accept(JCTree.java:669) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:431) 在com.sun.tools.javac.comp.Attr.attribTree(Attr.java:418) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:480) at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:3250) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:3173) at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:3109) at com.sun.tools.javac.comp.Attr.attrib(Attr.java:3083) at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1184) at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:870) at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:829) 在com.sun.tools.javac.main.Main.compile(Main.java:439) 在com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132) 在org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:45) 在org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:33) 在org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:101) 在org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:50) 在org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:36) 在org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:34) 在org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25) 在org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:157) 在org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:137) 在org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:91) at sun.reflect.GeneratedMethodAccessor1481.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:243) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:230) 在org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory $ StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80) 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61) 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46) 在org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35) 在org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64) 在org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 在org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42) 在org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52) 在org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53) 在org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) 在org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:310) 在org.gradle.execution.taskgraph.AbstractTaskPlanExecutor $ TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79) at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor $ TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63) at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor $ TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51) 在org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23) 在org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88) 在org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37) 在org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62) 在org.gradle.execution.DefaultBuildExecuter.access $ 200(DefaultBuildExecuter.java:23) 在org.gradle.execution.DefaultBuildExecuter $ 2.proceed(DefaultBuildExecuter.java:68) 在org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32) 在org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62) 在org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55) 在org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149) 在org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106) 在org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86) at org.gradle.launcher.exec.InProcessBuildActionExecuter $ DefaultBuildController.run(InProcessBuildActionExecuter.java:90) 在org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:54) 在org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41) at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28) 在org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:49) 在org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.ForwardClientInput $ 2.call(ForwardClientInput.java:74) 在org.gradle.launcher.daemon.server.exec.ForwardClientInput $ 2.call(ForwardClientInput.java:72) 在org.gradle.util.Swapper.swap(Swapper.java:38) 在org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66) 在org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:71) 在org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41) 在org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) 在org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy $ 1.run(StartBuildOrRespondWithBusy.java:50) 在org.gradle.launcher.daemon.server.DaemonStateCoordinator $ 1.run(DaemonStateCoordinator.java:246) at org.gradle.internal.concurrent.ExecutorPolicy $ CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54) at org.gradle.internal.concurrent.StoppableExecutorImpl $ 1.run(StoppableExecutorImpl.java:40) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
您是在尝试创建泛型类,还是构造函数采用泛型参数的具体类?
如果您尝试创建一个泛型类,可以为扩展AppCompatActivity & DialogInterface.OnDismissListener
的对象实例化,则需要使该类具有通用性(如编辑中所示)。在泛型类中,类型参数不需要在构造函数定义中重复,它们由隐式编译器承担。
如果您尝试创建一个具体或泛型类的类,其构造函数可以接受扩展AppCompatActivity & DialogInterface.OnDismissListener
的对象,则需要一种方法将该参数显式地转换为任何类型this.activity
定义为。将通用activity
参数分配给this.activity
而不进行转换的唯一方法是在类型擦除后它们是相同的类型。
虽然可以创建一个参数是通用的构造函数,但是不能在不进行转换的情况下将泛型参数分配给另一个类型(在类型擦除之后)。