Java 无法解析模板“类”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45497266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:04:14  来源:igfitidea点击:

Unable to parse template "Class"

javaandroidandroid-studio

提问by Hanan

I am trying to create class in android but i got this error

我正在尝试在 android 中创建类,但出现此错误

Unable to parse template "Class" Error message: This template did not produce a Java class or an interface

无法解析模板“类”错误消息:此模板未生成 Java 类或接口

and in Event log I got this error

在事件日志中我收到了这个错误

FileNotFoundException: Entry fileTemplates//code/Google Test Fixture SetUp Method.cc.ft not found in /D:/Android%20Studio/lib/idea.jar

FileNotFoundException: Entry fileTemplates//code/Google Test Fixture SetUp Method.cc.ft 在 /D:/Android%20Studio/lib/idea.jar 中找不到

this my app bulid gradle

这是我的应用程序 bulid gradle

apply plugin: 'com.android.application'

android
  {

compileSdkVersion 23
buildToolsVersion '25.0.0'

defaultConfig {
    applicationId "com.hh.ss.msed"
    minSdkVersion 14
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true


}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.firebase:firebase-core:11.0.1'
    compile 'com.google.firebase:firebase-database:11.0.1'
    compile 'com.google.firebase:firebase-crash:11.0.1'
    compile 'com.google.firebase:firebase-storage:11.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.0'
    compile 'com.android.support:cardview-v7:23.0.0'
    compile 'com.firebaseui:firebase-ui-database:0.6.2'


    compile 'com.firebase:firebase-client-android:2.4.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:support-v4:23.0.0'


}


packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
   }
  }
  apply plugin: 'com.google.gms.google-services'

this bulid gradle project

这个 bulid gradle 项目

// Top-level build file where you can add configuration options common to all sub-projects/modules.

  buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.1.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
 }

 allprojects {
  repositories {
    jcenter()
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

what the wrong?

怎么了?

回答by mgfernan

Hi I had the same error and the error was due to a bad syntax in the Java File header template. I solved it in the following way:

嗨,我遇到了同样的错误,该错误是由于 Java 文件头模板中的语法错误造成的。我通过以下方式解决了它:

Go to:

去:

Android Studio -> Preferences -> File and Code Templates -> Includes -> File Header

If you have defined some variables in the template, check how the variables are defined, should be (for instance):

如果您在模板中定义了一些变量,请检查变量是如何定义的,应该是(例如):

#set( $USER = "Your Name")

instead of

代替

#set( USER  "Your Name")

I hope it helps

我希望它有帮助

回答by SolderingIronMen

The reason is you maybe installed the 64 bit version on a 32 bit machine. I had the same problem. If you have 32-bit OS, try to uninstall 64-bit version of Android Studio and install 32-bit version from this page: https://developer.android.com/studio/index.html#downloads

原因是您可能在 32 位机器上安装了 64 位版本。我有同样的问题。如果您有 32 位操作系统,请尝试卸载 64 位版本的 Android Studio 并从此页面安装 32 位版本:https: //developer.android.com/studio/index.html#downloads

回答by Semicolon

I encountered something like this in android studio. What i did was I opened IntelliJ Idea (Since Android studio is based on it), and copied the default text of respective template and pasted in android studio respective file.

我在android studio中遇到过这样的事情。我所做的是打开 IntelliJ Idea(因为 Android Studio 基于它),并复制相应模板的默认文本并粘贴到 android studio 相应文件中。

Go to File->setting->editor->file and code templates, select Class in the files tab and write this

转到文件->设置->编辑器->文件和代码模板,在文件选项卡中选择类并写下这个

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { } 

回答by Angel Castelo

I have the same problem, and got it solved by adding in the idea.vmoptionsor idea64.vmoptionsthe following line:

我有同样的问题,并通过添加idea.vmoptionsidea64.vmoptions以下行来解决它:

-Djdk.util.zip.ensureTrailingSlash=false

as showed in JetBrains Support by user Wojciech Fred

用户 Wojciech FredJetBrains Support 中所示

Seems to be a fix applied by oracle to the Java 8 Update 144

似乎是 oracle 应用到 Java 8 Update 144 的修复程序

I hope it helps :D

我希望它有帮助:D

Edit: In Android Studio, the file is called studio.vmoptions, and can be found inside the configuration directory, but it is more easy if you go to the "help>Edit custom VM options" menu: Android Studio Configuration

编辑:在 Android Studio 中,该文件名为 studio.vmoptions,可以在配置目录中找到,但如果您转到“帮助>编辑自定义 VM 选项”菜单,则更容易: Android Studio 配置

回答by AMOGH KALYANSHETTI

the trick by Semicolon works Similarly for adding interfaces go to

分号的技巧同样适用于添加接口转到

Android Studio -> Preferences -> File and Code Templates -> Files -> Interface

and add the following code

并添加以下代码

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public interface ${NAME} { }

回答by Da Man

Go to File->setting->editor->file and code templates, select Class in the files tab and paste this:

转到文件->设置->编辑器->文件和代码模板,在文件选项卡中选择类并粘贴:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { }

Screenshot of editor

编辑器截图

回答by Alex A. Adusei

In your, IntelliJ IDE go to Help => Edit Custom VM Options and Add to your idea.vmoptions or idea64.vmoptions the following line:

在您的 IntelliJ IDE 中,转到 Help => Edit Custom VM Options 并将以下行添加到您的 idea.vmoptions 或 idea64.vmoptions:

-Djdk.util.zip.ensureTrailingSlash=false

-Djdk.util.zip.ensureTrailingSlash=false

You can read more about it hereand here.

您可以在此处此处阅读有关它的更多信息。

回答by Johnett Mathew

I didn't encounter this error before. I faced this issue only after I reinstall my android studio(version 3.0). Creating new Class give the following error as mentioned in the question.

我之前没有遇到过这个错误。我只有在重新安装我的 android studio(3.0 版)后才遇到这个问题。创建新类会出现问题中提到的以下错误。

Unable to parse template "Class" Error message: This template did not produce a Java class or an interface

无法解析模板“类”错误消息:此模板未生成 Java 类或接口

So there are two ways to get rid of this error

所以有两种方法可以摆脱这个错误

Option 1 is @Semicolon answer

选项 1 是@Semicolon 答案

Go to File->Setting->Editor->File and Code Templates, select Class in the files tab and write this

转到File->Setting->Editor->File and Code Templates,在 files 选项卡中选择 Class 并写下这个

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") public class ${NAME} { }

Option 2 is @Angel Castelo answer

选项 2 是@Angel Castelo 的回答

Add the following line to your VM custom option(studio.vmoptions).

将以下行添加到您的 VM 自定义选项 (studio.vmoptions)。

-Djdk.util.zip.ensureTrailingSlash=false

My suggestion is to add it using android studio(Select Help->Edit Custom VM Options->then add the code). If you didn't create Custom VM Option File before then it will prompt a dialogue mentioning "Custom VM Option file not exist. do you want to create it" Select yes and the studio will create a Custom VM option file. Then follow the steps(above mentioned).

我的建议是使用 android studio 添加它(选择 Help->Edit Custom VM Options-> then add the code)。如果您之前没有创建自定义 VM 选项文件,它会提示一个对话框,其中提到“自定义 VM 选项文件不存在。是否要创建它”选择是,工作室将创建一个自定义 VM 选项文件。然后按照步骤(上面提到的)操作。

Conclusion

结论

Which one is better?

哪一个更好?

When I first encounter this issue I choose option one and its worked well until I started my next project. When I started a new project the issue appear again so I visit this page and found option 2and it worked perfectly for me.

当我第一次遇到这个问题时,我选择了第一个选项,它运行良好,直到我开始下一个项目。当我开始一个新项目时,问题再次出现,所以我访问了这个页面并找到了选项 2,它对我来说非常有效。

When we follow option 2creating new project doesn't trigger this issue again unlike option 1.

选项 1不同,当我们遵循选项 2创建新项目时,不会再次触发此问题。

Further Reading

进一步阅读

Intellij Support Page

Intellij 支持页面

Android Developer Page

Android 开发者页面

Read more about Android Studio file templates

阅读有关 Android Studio 文件模板的更多信息

回答by Vikrant Shah

This all answer may be right, and work very fine, I use this options for few days and it's work for me too, but there are one other option and i think which is much better because, this option is given by android developerwhich is as give as below:

这一切的答案可能是正确的,而且工作得很好,我使用这个选项几天,它也对我有用,但还有一个其他选项,我认为哪个更好,因为这个选项是由android 开发人员提供的,它是如下:

For Class file template:

对于类文件模板:

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

    #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
    #end
    #parse("File Header.java")

    #if (${VISIBILITY} == "PUBLIC")public #end #if (${ABSTRACT} == "TRUE")abstract #end #if (${FINAL} == "TRUE")final #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end {
    }

Interface file template

接口文件模板

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end {
#end {
}

enter image description here

在此处输入图片说明

回答by Arlind

I had the same issue, it just appeared I didn't change any settings or update Android studio what so ever. I solved it by selecting

我遇到了同样的问题,似乎我没有更改任何设置或更新 Android Studio。我通过选择解决了它

File -> Invalidate Caches & Restart