Android Studio:导航xml编辑和设计视图问题 [英] Android Studio: Issue with navigation xml editing and design view

查看:87
本文介绍了Android Studio:导航xml编辑和设计视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在项目中使用Navigation组件,但偶然发现了一个问题,该问题似乎对我们的项目和JetBrains Android插件来说是唯一的.我无法确切指出它开始发生的确切点,但是在AS 3.2发行后我注意到了这一点.

We've been using the Navigation component in our project but have stumbled across an issue which seems unique to our project and the JetBrains Android plugin. I can't pinpoint the exact point it started happening but I noticed it after the release of AS 3.2.

该项目构建良好,但是在编辑导航XML时,自动完成和突出显示之类的内容已损坏,Android支持插件抛出此错误:

The project builds fine however when editing a navigation XML things such as auto-complete and highlighting are broken and the Android Support Plugin throws this error:

java.lang.IllegalArgumentException: Multiple entries with same key: org.jetbrains.android.dom.navigation.NavigationSchema$TypeRef@0=org.jetbrains.android.dom.navigation.NavigationSchema$NavigatorKeyInfo@17d69e53 and org.jetbrains.android.dom.navigation.NavigationSchema$TypeRef@0=org.jetbrains.android.dom.navigation.NavigationSchema$NavigatorKeyInfo@442c4fde
at com.google.common.collect.ImmutableMap.conflictException(ImmutableMap.java:215)
at com.google.common.collect.ImmutableMap.checkNoConflict(ImmutableMap.java:209)
at com.google.common.collect.RegularImmutableMap.checkNoConflictInKeyBucket(RegularImmutableMap.java:147)
at com.google.common.collect.RegularImmutableMap.fromEntryArray(RegularImmutableMap.java:110)
at com.google.common.collect.ImmutableMap$Builder.build(ImmutableMap.java:393)
at org.jetbrains.android.dom.navigation.NavigationSchema.buildCacheKeys(NavigationSchema.java:488)
at org.jetbrains.android.dom.navigation.NavigationSchema.init(NavigationSchema.java:479)
at org.jetbrains.android.dom.navigation.NavigationSchema.createIfNecessary(NavigationSchema.java:389)
at org.jetbrains.android.dom.AttributeProcessingUtil.processNavAttributes(AttributeProcessingUtil.java:408)
at org.jetbrains.android.dom.AttributeProcessingUtil.processAttributes(AttributeProcessingUtil.java:596)
at org.jetbrains.android.dom.AndroidDomExtender.registerExtensions(AndroidDomExtender.java:57)
at org.jetbrains.android.dom.AndroidDomExtender.registerExtensions(AndroidDomExtender.java:29)
at com.intellij.util.xml.reflect.DomExtenderEP.extend(DomExtenderEP.java:83)
at com.intellij.util.xml.impl.DynamicGenericInfo.runDomExtenders(DynamicGenericInfo.java:134)
at com.intellij.util.xml.impl.DynamicGenericInfo.lambda$checkInitialized$0(DynamicGenericInfo.java:64)
at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:98)
at com.intellij.util.xml.impl.DynamicGenericInfo.checkInitialized(DynamicGenericInfo.java:63)
at com.intellij.util.xml.impl.DynamicGenericInfo.getAttributeChildrenDescriptions(DynamicGenericInfo.java:241)
at com.intellij.util.xml.impl.DynamicGenericInfo.processAttributeChildrenDescriptions(DynamicGenericInfo.java:254)
at com.intellij.util.xml.impl.DomSemContributor.lambda$registerSemProviders$5(DomSemContributor.java:199)
at com.intellij.semantic.SemServiceImpl$2.lambda$registerSemElementProvider$0(SemServiceImpl.java:93)
at com.intellij.semantic.SemServiceImpl.createSemElements(SemServiceImpl.java:190)
at com.intellij.semantic.SemServiceImpl.getSemElements(SemServiceImpl.java:161)
at com.intellij.semantic.SemService.getSemElement(SemService.java:37)
at com.intellij.util.xml.impl.DomManagerImpl.getDomHandler(DomManagerImpl.java:390)
at com.intellij.util.xml.impl.GenericValueReferenceProvider.getReferencesByElement(GenericValueReferenceProvider.java:47)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.getReferences(ReferenceProvidersRegistryImpl.java:135)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.mapNotEmptyReferencesFromProviders(ReferenceProvidersRegistryImpl.java:123)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.doGetReferencesFromProviders(ReferenceProvidersRegistryImpl.java:102)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:50)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:44)
at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.getReferences(XmlAttributeValueImpl.java:110)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.checkReferences(XmlHighlightVisitor.java:439)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.visitXmlAttributeValue(XmlHighlightVisitor.java:406)
at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.accept(XmlAttributeValueImpl.java:61)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.visit(XmlHighlightVisitor.java:587)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.runVisitors(GeneralHighlightingPass.java:353)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$collectHighlights$5(GeneralHighlightingPass.java:286)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:313)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$analyzeByVisitors$6(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.analyze(XmlHighlightVisitor.java:597)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$analyzeByVisitors$6(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.analyze(DefaultHighlightVisitor.java:71)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectHighlights(GeneralHighlightingPass.java:283)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectInformationWithProgress(GeneralHighlightingPass.java:227)
at com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.doCollectInformation(ProgressableTextEditorHighlightingPass.java:84)
at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:69)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$null$1(PassExecutorService.java:423)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1171)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$2(PassExecutorService.java:416)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:582)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:532)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:87)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.doRun(PassExecutorService.java:415)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$run$0(PassExecutorService.java:391)
at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:147)
at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:222)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:389)
at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask$1.exec(JobLauncherImpl.java:161)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

我们正在运行导航组件的最新版本(2.0.0-rc2),并且我尝试了所有常见的修复方法(无效缓存和重新启动等).我曾尝试在新项目中使用导航,但似乎可以正常运行,而不会抛出此错误.

We're running the latest version of the navigation component (2.0.0-rc2) and and I've tried all the usual fixes (Invalidate Caches & Restart etc). I've tried using navigation in fresh projects and it seems to work without this error being thrown.

我尝试从我们的项目中删除所有导航XML,然后添加一个完全空白的XML,但它仍然会引发错误.我研究了插件的源代码,似乎是在插件最初为设计视图处理导航文件时发生的,如果将它扔给新的导航文件则没有意义.

I have tried removing all nav XMLs from our project and then adding in a completely blank XML but it still throws the error. I delved into the source of the plugin and it seems this occurs when the plugin is initially processing the nav file for the design view which doesn't make sense if it's throwing it for a fresh nav file.

我现在不知道为什么会发生这种情况,任何想法都将不胜感激!

I'm currently at a loss as to why this may be happening and any ideas would be greatly appreciated!

推荐答案

简短答案:

我通过将扩展了androidx.navigation.Navigator的匿名类移至单独的类并添加具有唯一名称的注释@Navigator.Name(String name)来解决此问题.

I fixed it by moving an anonymous class which extended androidx.navigation.Navigator to a separate class and adding the annotation @Navigator.Name(String name) with a unique name.

如果您也遇到此问题,请检查项目中或其依赖项中是否有任何继承Navigator类的类,这些类缺少@Navigator.Name(String name)批注或使用非唯一字符串作为name参数. fragmentactivity不能使用,因为它们已在API中使用.

If you are also experiencing this issue check that in your project or its dependencies if there are any classes inheriting the Navigator class which are either missing the @Navigator.Name(String name) annotation or using non-unique strings as the name parameter. fragment and activity cannot be used as they are used already within the API.

其他信息:

在进一步研究Android支持插件源代码以了解发生了什么之后,我设法解决了我们项目中的问题.

After further delving into the Android Support plugin source code to understand what's going on I've managed to solve the issue on our project.

在Android支持插件中启动IDE功能时,它会遍历您的项目和所有依赖项,以查找继承Navigator类的任何类并为其提供标签,以便在XML文件和设计视图中对其进行管理.该标签基于注释@Navigator.Name(String name).如果没有注释,则使用默认值.

When initiating the IDE functionality in the Android Support Plugin it goes through your project and any dependencies looking for any class which inherits the Navigator class and gives it a tag for the purpose of managing it in the XML files and design view. This tag is based on an annotation @Navigator.Name(String name). If no annotation is present a default value is used.

这些标签必须是唯一的,因为它们被用作构建标签到导航器类型的不变映射的键.在我们的项目中,我们有一个匿名类,该类扩展了Navigator类并且没有注释.

These tags have to be unique as they are used as the keys for building an immutable map of tags to navigator types. In our project we had an anonymous class which was extending Navigator class and had no annotation.

导航组件源中,特别是在NavDeepLinkBuilder类中,还有一个扩展Navigator的匿名类.由于标签只有一个默认值,因此任何其他继承Navigator的类都不会在您的项目或其依赖项中出现任何注释,就会导致此问题.

There is also an anonymous class extending Navigator within the navigation component source, specifically within the NavDeepLinkBuilder class. As there is only a single default value for the tags any other class inheriting Navigator with no annotation being present in your project or its dependencies will cause this issue.

这篇关于Android Studio:导航xml编辑和设计视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆