Android 初始化 Cordova 时出错:找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11934974/
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-20 09:13:47  来源:igfitidea点击:

Error initializing Cordova: Class not found

androidcordova

提问by Nilanchal

I followed the tutorial of Phonegap from the official Phonegap.com site (getting started with Android).

我遵循了 Phonegap.com 官方网站上的 Phonegap 教程(Android 入门)。

I have created project with following all the steps.

我已经按照所有步骤创建了项目。

  1. created activity and extended from "DroidGap" and added loadURL method from onCreate().
  2. Added cordova-2.0.0.js and cordova-2.0.0.jar on the respective folder.
  3. Added the jar file to the build path
  4. Loaded the js file from the html tag
  5. Added permissions from AndroidMainfeast.xml
  6. copied the xml folder containing "configs.xml"
  1. 创建活动并从“DroidGap”扩展并从 onCreate() 添加 loadURL 方法。
  2. 在相应的文件夹中添加了cordova-2.0.0.js 和cordova-2.0.0.jar。
  3. 将 jar 文件添加到构建路径
  4. 从 html 标签加载 js 文件
  5. 从 AndroidMainfeast.xml 添加权限
  6. 复制了包含“configs.xml”的xml文件夹

Now I don't understand where's the problem. It is showing the below error as shown in the screenshot enter image description here

现在我不明白问题出在哪里。它显示以下错误,如屏幕截图所示 在此处输入图片说明

Here is my HTML file

这是我的 HTML 文件

<!DOCTYPE html>
<html>
  <head>
    <title>Notification Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Cordova is ready
    //
    function onDeviceReady() {
        // Empty
    }

    // Show a custom alert
    //
    function showAlert() {
        navigator.notification.alert(
            'You are the winner!',  // message
            'Game Over',            // title
            'Done'                  // buttonName
        );
    }

    // Beep three times
    //
    function playBeep() {
        navigator.notification.beep(3);
    }

    // Vibrate for 2 seconds
    //
    function vibrate() {
        navigator.notification.vibrate(2000);
    }

    </script>
  </head>
  <body>
    <p><a href="#" onclick="showAlert()">Show Alert</a></p>
    <p><a href="#" onclick="playBeep()">Play Beep</a></p>
    <p><a href="#" onclick="vibrate()">Vibrate</a></p>
  </body>
</html>

Any advice? Thanks!

有什么建议吗?谢谢!

回答by Simon MacDonald

It is config.xmlnot configs.xml.

这是config.xml不是configs.xml

Based on the logs I'm seeing you are missing this file in your apps res/xmldirectory. It is included as part of the distribution at lib/android/res/xml/config.xmlso copy that file into your res/xmldirectory and you should be all set.

根据日志,我看到您的应用程序res/xml目录中缺少此文件。它作为发行版的一部分包含在内,lib/android/res/xml/config.xml因此将该文件复制到您的res/xml目录中,您应该已经全部设置好了。

回答by bflesch

Just as aharris88 in his answer to this question, I had the [CORDOVA] Error initilizing Cordova: Class not founderror message when using Cordova 3.1.0on my Android dev phone after migrating from Phonegap 3.0.

正如aharris88 在他对这个问题的回答中一样,从 Phonegap 3.0 迁移后,在我的 Android 开发手机上[CORDOVA] Error initilizing Cordova: Class not found使用Cordova 3.1.0时,我收到了错误消息。

All fiddling with the config.xmlfile in the /platforms/android/res/xmldirectory did not help. Based on the Stackoverflow answer mentioned above, I tried to "start over" by reinstalling Android platform support:

所有摆弄目录中的config.xml文件都/platforms/android/res/xml无济于事。基于上面提到的 Stackoverflow 答案,我尝试通过重新安装 Android 平台支持来“重新开始”:

cordova platform rm android
cordova platform add android

After this step it worked again, and I was able to cordova build android && cordova run androidwithout any further problems.

在这一步之后它再次工作,我能够cordova build android && cordova run android没有任何进一步的问题。

回答by Kazmin

I have had this error because of a plugin that I deleted. I Added

由于我删除了一个插件,我遇到了这个错误。我添加了

<plugin name="Device" value="org.apache.cordova.Device"/>

to the config.xml file again and it fixed it.

再次到 config.xml 文件并修复它。

回答by Pratik Butani

My you not have following plugin:

我的你没有以下插件:

<plugin name="Device" value="org.apache.cordova.Device"/>

You have to put that in between <plugins></plugins>. like

你必须把它放在中间<plugins></plugins>。喜欢

<plugins>
    <plugin name="Device" value="org.apache.cordova.Device"/>
</plugins>

回答by aharris88

I had this problem using phonegap 3.x and the problem turned out to be that phonegap hadn't properly installed the plugins, or they just messed up along the way. Basically when you install the plugins and build for a platform it takes the javascript files from plugins/org.apache.cordova.core.specific-plugin/www and puts them in platforms/android/assets/www/plugins/org.apache.cordova.core.specific-plugin/www and then it takes the Java files (or objective C for iOS) and puts them in platforms/android/src/org/apache/cordova/specificplugin

我在使用 phonegap 3.x 时遇到了这个问题,结果是 phonegap 没有正确安装插件,或者他们只是一路搞砸了。基本上,当您安装插件并为平台构建时,它会从 plugins/org.apache.cordova.core.specific-plugin/www 获取 javascript 文件并将它们放在platforms/android/assets/www/plugins/org.apache 中。 cordova.core.specific-plugin/www 然后它获取 Java 文件(或 iOS 的目标 C)并将它们放在platforms/android/src/org/apache/cordova/specificplugin

And all of this is specified by plugins/org.apache.cordova.core.specific-plugin/plugin.xml. If you look in a plugins.xml you should see something like:

所有这些都由 plugins/org.apache.cordova.core.specific-plugin/plugin.xml 指定。如果您查看 plugins.xml,您应该看到如下内容:

<source-file src="src/android/NetworkManager.java" target-dir="src/org/apache/cordova/networkinformation" />

So this tells you that in platforms/android/src/org/apache/cordova/networkinformation, there should be NetworkManager.java. And that file can be copied from plugins/org.apache.cordova.core.network-information/src/android/NetworkManager.java

所以这告诉你在platforms/android/src/org/apache/cordova/networkinformation,应该有NetworkManager.java。该文件可以从 plugins/org.apache.cordova.core.network-information/src/android/NetworkManager.java 复制

Now all of this is supposed to happen automatically without having to touch the platforms folder. But if it messes up you can fix it by manually copying the correct files into the correct folders.

现在所有这些都应该自动发生,而无需触及平台文件夹。但是如果它搞砸了,您可以通过手动将正确的文件复制到正确的文件夹中来修复它。

回答by Eric

Well,the error I found is:

好吧,我发现的错误是:

05-28 08:19:53.139: E/PluginManager(1478): ===================================================================================== 05-28 08:19:53.139: E/PluginManager(1478): ERROR: config.xml is missing. Add res/xml/config.xml to your project. 05-28 08:19:53.139: E/PluginManager(1478): https://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=blob;f=framework/res/xml/plugins.xml

05-28 08:19:53.139: E/PluginManager(1478): ================================== ================================================== = 05-28 08:19:53.139: E/PluginManager(1478): 错误: config.xml 丢失。将 res/xml/config.xml 添加到您的项目中。05-28 08:19:53.139: E/PluginManager(1478): https://git-wip-us.apache.org/repos/asf?p=incubator-cordova-android.git;a=blob;f=框架/res/xml/plugins.xml

but I found /res/xml/config.xml in my project

但我在我的项目中找到了 /res/xml/config.xml

finally,I found error in org.apache.cordova.api.PluginManager:

最后,我在 org.apache.cordova.api.PluginManager 中发现错误:

public void loadPlugins() {
int id = this.ctx.getActivity().getResources().getIdentifier("config", "xml",    this.ctx.getActivity().getClass().getPackage().getName());
...

should change to:

应该改为:

public void loadPlugins() {
int id = this.ctx.getActivity().getResources().getIdentifier("config", "xml",    this.ctx.getActivity().getPackageName());
...

you could read more about the method "getIdentifier(String name, String defType, String defPackage)" in offical doc

您可以在官方文档中阅读有关“getIdentifier(String name, String defType, String defPackage)”方法的更多信息

回答by Roger

You can resolve this when using phonegap command line by doing the following:

您可以在使用 phonegap 命令行时通过执行以下操作来解决此问题:

  1. Delete merges/android, platforms/android, plugins/android.json

  2. run phonegap local build android

  1. 删除合并/android、平台/android、插件/android.json

  2. phonegap local build android

回答by John Kelleher

Had same problem with Class not found. One problem to look at is to ensure that the android.json (or ios.json) file is being updated on a build. Mine was effectively empty. Also, remove and add back the plugins as mentioned in some other posts. Finally, the thing that worked for me was to ensure that the plugins were corrected referenced in the config.xml:

与 Class not found 有同样的问题。要查看的一个问题是确保在构建时更新 android.json(或 ios.json)文件。我的实际上是空的。此外,删除并重新添加一些其他帖子中提到的插件。最后,对我有用的是确保在 config.xml 中引用的插件得到纠正:

<feature name="Device">
  <param name="android-package" value="org.apache.cordova.device.Device"/>
</feature>
<feature name="Camera">
  <param name="android-package" value="org.apache.cordova.camera.CameraLauncher"/>
</feature>
<feature name="Notification">
  <param name="android-package" value="org.apache.cordova.dialogs.Notification"/>
</feature>

Note the double naming of 'device.Device' and also the sub-classing of the 'camera' and 'dialogs' plugins. This just isn't properly referenced by the Phonegap documentation.

请注意“device.Device”的双重命名以及“camera”和“dialogs”插件的子分类。这只是 Phonegap 文档没有正确引用。

回答by Pranay Dutta

So After near to kill myself , i found that i was using cordova version 5.3.3 and cordova-android version 5.0.0, somehow i don't know may be there are not compatible or there can be a bug ,so i fall back to cordova android 4.1.1

所以在接近自杀后,我发现我使用的是cordova 5.3.3版和cordova-android 5.0.0版,不知何故我不知道可能是不兼容还是可能有错误,所以我退缩了到科尔多瓦安卓 4.1.1

  cordova platform rm android
  cordova platform add [email protected] 

and this saved me up

这救了我

回答by Chris

I ran into the same error: "Error initializing Cordova: Class not found", using cordova with visual studio 2015. But this wasn't the only error, none of my plugins seem to get trough. I've tried about EVERYTHING. From cordova platform rm/add androidx1000 times, to deleting and re-adding the plugins manually, nothing seem to do the trick.

我遇到了同样的错误:“初始化 Cordova 时出错:找不到类”,在 Visual Studio 2015 中使用了cordova。但这不是唯一的错误,我的插件似乎都没有通过。我已经尝试了一切。从cordova platform rm/add androidx1000 次,到手动删除和重新添加插件,似乎没有任何办法。

enter image description here

在此处输入图片说明

Then i changed my cordova-cli in the taco.json file from 5.3.3 to 5.4.0 and ran it on my device. Finally this fixed the whole issue for me. I then downgraded the cordova version back to 5.3.3 ( the 5.4.0 version is not supported by the adb bridge as of yet ). Try it out!

然后我将 taco.json 文件中的 cordova-cli 从 5.3.3 更改为 5.4.0 并在我的设备上运行它。最后,这为我解决了整个问题。然后我将cordova 版本降级回5.3.3(adb 网桥目前还不支持5.4.0 版本)。试试看!