来自PluginManager的NullPointer异常 - 创建自定义Apache Cordova插件Echo.java

时间:2015-07-31 13:59:07

标签: javascript android apache cordova cordova-plugins

我一直在尝试从现有的Echo插件创建示例插件。我一直在收到以下错误:

W/System.err(1894): java.lang.NullPointerException: cls == null
W/System.err(1894):     at java.lang.Class.isAssignableFrom(Native Method)
W/System.err(1894):     at org.apache.cordova.PluginManager.instantiatePlugin(PluginManager.java:489)
W/System.err(1894):     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:167)
W/System.err(1894):     at org.apache.cordova.PluginManager.exec(PluginManager.java:120)
W/System.err(1894):     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
W/System.err(1894):     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
W/System.err(1894):     at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err(1894):     at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
W/System.err(1894):     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err(1894):     at android.os.Looper.loop(Looper.java:136)
W/System.err(1894):     at android.os.HandlerThread.run(HandlerThread.java:61)
I/System.out(1894): Error adding plugin .
W/System.err(1894): java.lang.NullPointerException
W/System.err(1894):     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:169)
W/System.err(1894):     at org.apache.cordova.PluginManager.exec(PluginManager.java:120)
W/System.err(1894):     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
W/System.err(1894):     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
W/System.err(1894):     at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/System.err(1894):     at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
W/System.err(1894):     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err(1894):     at android.os.Looper.loop(Looper.java:136)
W/System.err(1894):     at android.os.HandlerThread.run(HandlerThread.java:61)

以下是我的文件:

Echo.java

package com.gerald.example.sampleecho;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

public class Echo extends CordovaPlugin {

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView)     {
        // TODO Auto-generated method stub
        super.initialize(cordova, webView);
        Log.d("SAMPLE_ECHO", "initializing");
    }

    @Override
    public boolean execute(String action, JSONArray args,
            CallbackContext callbackContext) throws JSONException {
        Log.d("SAMPLE_ECHO", action);
        if(action.equals("echo")){
            String message = args.getString(0);
            this.echo(message, callbackContext);
            return true;
        }

        return false;
    }



    private void echo(String message, CallbackContext callbackContext){
        Log.d("SAMPLE_ECHO", "Echoing...");
        if(message != null && message.length()>0){
                callbackContext.success(message);
        }else{
                callbackContext.error("Expected one non-empty string      argument.");
        }
    }
}

plugin.xml如下所示

的plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.gerald.example.sampleecho.echo"
version="1.2.2">

<name>SampleEcho</name>

<description>Echoes stuff</description>
<author>Oyudo Gerald</author>
<js-module src="www/echo.js" name="echo" >
    <clobbers target="myEcho"/>
</js-module>
<engines>
    <engine name="cordova" version="4.0.2"/>
</engines>
<!-- android -->
    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Echo"
               value="com.gerald.example.sampleecho.Echo"/>
        </config-file>

        <source-file src="src/com/gerald/example/sampleecho/Echo.java"
                target-dir="src/com/gerald/example/sampleecho" />
    </platform>
</plugin>

和HTML代码

的index.html

<html>
<head>
    <title>HelloWorld!</title>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy" content="default-src *;   style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">        
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript">
        function onBodyLoad(){
            document.addEventListener("deviceready",onDeviceReady, false);
        }
        function onDeviceReady(){
             br = "<br />";         
             //Get the appInfo DOM element        
              var element = document.getElementById("appInfo");         
              ////Replace it with specific information about the device         
              ////running the application         
              element.innerHTML = 'Cordova Version: ' +            
                      device.cordova + br +            
                      'Platform: ' + device.platform + br +          
                      'Model: ' + device.model + br +           
                      'OS Version: ' + device.version;        

        }
        function doEcho(){
             myEcho.echo("Plugin installed successfully", function(message){
                  alert(message);
              });
        }
    </script>
</head>
    <body onload="onBodyLoad()">
        <h1>Hello World</h1>
        <p>This is a sample Cordova application</p>
        <p id ="appInfo">
            waiting for cordova initialization to complete
        </p>
        <button onclick="doEcho();">Please Echo</button>
    </body>
</html>

我使用的是cordova版本4.0.2和JDK 8。 堆栈跟踪没有引用我的代码的部分,所以我甚至不知道从哪里开始。我哪里做错了?

我在eclipse调试模式下运行了android项目,我注意到我的插件类的字符串表示被传递给了CordovaWebViewImpl并且传递给PluginManager,我想这就是错误的来源。新问题是为什么我的插件的pluginEntry在从CordovaInterface通过init()方法传递给CordovaWebViewImpl时为null?

1 个答案:

答案 0 :(得分:2)

和...经过一整天的尝试,我终于通过研究ConfigXMLParser找到了以下插件的功能格式无法读取。

<feature name="Echo"
           value="com.gerald.example.sampleecho.Echo"/>

改为使用以下内容:

<feature name="Echo">
    <param name="android-package" value="com.gerald.example.sampleecho.Echo" />
</feature>