2013-04-15 68 views
0

我的html代碼/資產/ WWWPhoneGap的Android原生功能沒有得到所謂的

<!DOCTYPE html> 
<\html> 

<\head> 

<\title>FileReader Example</title> 

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

function onLoad() { 
    console.log("in onLoad"); 
    document.addEventListener("deviceready", onDeviceReady, false); 
    execute(success, fail, "Echo", action, [args,"1","2"]); 
    console.log("in at end of onLoad"); 
} 


function onDeviceReady() { 
    console.log("in onDeviceReady"); 
} 


function success(file){ 
    console.log("success"); 
} 

function fail(evt) { 
    console.log("in fail"); 
    console.log(evt.target.error.code); 
} 

<\/script> 
<\/head> 

<\body onload="onLoad()" > 

<\h1>Example</h1> 

<\p>Read File</p> 

<\/body> 

<\/html> 

我的Java類代碼加入config.xml文件中/ RES

package com.example.testphonegap; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.api.CordovaPlugin; 
import org.json.JSONArray; 
import org.json.JSONException; 

import android.util.Log; 

public class Echo extends CordovaPlugin { 
@Override 
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)  throws JSONException { 
    // if (action.equalsIgnoreCase("echo")) { 
    String message = args.getString(0); 
    this.echo(message, callbackContext); 
    Log.v("success", "success"); 
    return true; 
    // } 
    // return false; 
} 

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

線/ XML

<plugin name="Echo" value="com.example.testphonegap.Echo" /> 

我的Logcat

04-15 12:22:38.455: I/dalvikvm(778): Could not find method org.apache.cordova.CordovaWebView.setOverScrollMode, referenced from method org.apache.cordova.DroidGap.init 
04-15 12:22:38.455: W/dalvikvm(778): VFY: unable to resolve virtual method 1098: Lorg/apache/cordova/CordovaWebView;.setOverScrollMode (I)V 
04-15 12:22:38.455: D/dalvikvm(778): VFY: replacing opcode 0x6e at 0x0043 
04-15 12:22:38.615: I/CordovaLog(778): Changing log level to DEBUG(3) 
04-15 12:22:38.635: I/CordovaLog(778): Found preference for useBrowserHistory=true 
04-15 12:22:38.655: D/CordovaLog(778): Found preference for useBrowserHistory=true 
04-15 12:22:38.655: I/CordovaLog(778): Found preference for exit-on-suspend=false 
04-15 12:22:38.665: D/CordovaLog(778): Found preference for exit-on-suspend=false 
04-15 12:22:38.665: D/DroidGap(778): DroidGap.onCreate() 
04-15 12:22:38.725: I/dalvikvm(778): Could not find method android.webkit.WebView.<init>, referenced from method org.apache.cordova.CordovaWebView.<init> 
04-15 12:22:38.725: W/dalvikvm(778): VFY: unable to resolve direct method 344: Landroid/webkit/WebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V 
04-15 12:22:38.725: D/dalvikvm(778): VFY: replacing opcode 0x70 at 0x0001 
04-15 12:22:38.745: D/dalvikvm(778): VFY: dead code 0x0004-0046 in Lorg/apache/cordova/CordovaWebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V 
04-15 12:22:39.465: D/JsMessageQueue(778): Set native->JS mode to 2 
04-15 12:22:39.475: I/CordovaWebView(778): Disabled addJavascriptInterface() bridge since Android version is old. 
04-15 12:22:39.485: E/dalvikvm(778): Could not find class 'android.webkit.WebResourceResponse', referenced from method org.apache.cordova.CordovaWebViewClient.getWhitelistResponse 
04-15 12:22:39.525: W/dalvikvm(778): VFY: unable to resolve new-instance 129 (Landroid/webkit/WebResourceResponse;) in Lorg/apache/cordova/CordovaWebViewClient; 
04-15 12:22:39.525: D/dalvikvm(778): VFY: replacing opcode 0x22 at 0x000b 
04-15 12:22:39.525: D/dalvikvm(778): VFY: dead code 0x000d-0014 in Lorg/apache/cordova/CordovaWebViewClient;.getWhitelistResponse()Landroid/webkit/WebResourceResponse; 
04-15 12:22:39.585: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;) 
04-15 12:22:39.615: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;) 
04-15 12:22:39.615: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;) 
04-15 12:22:39.665: D/DroidGap(778): DroidGap.init() 
04-15 12:22:39.696: D/CordovaWebView(778): >>> loadUrl(file:///android_asset/www/library_access.html) 
04-15 12:22:39.725: D/PluginManager(778): init() 
04-15 12:22:39.765: D/CordovaWebView(778): >>> loadUrlNow() 
04-15 12:22:39.775: D/CordovaWebView(778): >>> loadUrl(file:///android_asset/www/file_reader.html) 
04-15 12:22:39.775: D/PluginManager(778): init() 
04-15 12:22:39.805: D/CordovaWebView(778): >>> loadUrlNow() 
04-15 12:22:39.805: D/DroidGap(778): Resuming the App 
04-15 12:22:39.915: D/DroidGap(778): onMessage(onPageStarted,file:///android_asset/www/library_access.html) 
04-15 12:22:39.955: D/SoftKeyboardDetect(778): Ignore this event 
04-15 12:22:40.084: D/Cordova(778): onPageFinished(file:///android_asset/www/library_access.html) 
04-15 12:22:40.084: D/Cordova(778): Trying to fire onNativeReady 
04-15 12:22:40.095: D/DroidGap(778): onMessage(onNativeReady,null) 
04-15 12:22:40.095: D/DroidGap(778): onMessage(onPageFinished,file:///android_asset/www/library_access.html) 
04-15 12:22:40.134: D/DroidGap(778): onMessage(onPageStarted,file:///android_asset/www/file_reader.html) 
04-15 12:22:40.225: D/SoftKeyboardDetect(778): Ignore this event 
04-15 12:22:41.184: D/dalvikvm(778): GC_FOR_MALLOC freed 2668 objects/187648 bytes in 208ms 
04-15 12:22:42.185: D/DroidGap(778): onMessage(spinner,stop) 
04-15 12:22:44.655: D/CordovaLog(778): in onLoad 
04-15 12:22:44.655: I/Web Console(778): in onLoad at file:///android_asset/www/file_reader.html:10 
04-15 12:22:44.695: D/CordovaLog(778): ReferenceError: Can't find variable: execute 
04-15 12:22:44.695: E/Web Console(778): ReferenceError: Can't find variable: execute at file:///android_asset/www/file_reader.html:12 
04-15 12:22:47.615: D/CordovaLog(778): Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only. 
04-15 12:22:47.615: I/Web Console(778): Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only. at file:///android_asset/www/cordova-2.6.0.js:906 
04-15 12:22:47.935: D/CordovaNetworkManager(778): Connection Type: 3g 
04-15 12:22:47.985: D/CordovaNetworkManager(778): Connection Type: 3g 
04-15 12:22:47.995: D/DroidGap(778): onMessage(networkconnection,3g) 
04-15 12:22:48.096: D/DroidGap(778): onMessage(spinner,stop) 
04-15 12:22:48.134: D/Cordova(778): onPageFinished(file:///android_asset/www/file_reader.html) 
04-15 12:22:48.134: D/Cordova(778): Trying to fire onNativeReady 
04-15 12:22:48.145: D/DroidGap(778): onMessage(onNativeReady,null) 
04-15 12:22:48.145: D/DroidGap(778): onMessage(onPageFinished,file:///android_asset/www/file_reader.html) 
04-15 12:22:48.335: D/CordovaLog(778): in onDeviceReady 
04-15 12:22:48.335: I/Web Console(778): in onDeviceReady at file:///android_asset/www/file_reader.html:18 
+0

你的onload函數什麼時候被調用? – wmfairuz

+0

@wmfairuz請看我更新的問題,你會知道的。我在html代碼中的每個標記之前添加了額外的\,因爲它沒有按原樣打印我的代碼。我認爲應該有一些方法來避免這種情況,因爲對於像我這樣的人,誰不知道html,它變得很難避免它 –

+0

我從來沒有見過這些dalvikvm錯誤消息,它讓我覺得可能是什麼你的配置有問題嗎?您是否將目標API級別設置爲最新的Android?另外,這行代碼:''ReferenceError:找不到變量:執行file:///android_asset/www/file_reader.html:12''讓我認爲在JavaScript中你應該調用''cordova.execute'' ''或''window.plugins.Echo()''?你有沒有創建,你可以粘貼Echo.js文件? Cordova插件通常應該有兩個文件,一個是Javascript一個,一個是本地文件。 – MBillau

回答

2

在onLoad之外放置此行document.addEventListener("deviceready", onDeviceReady, false);作爲腳本部分的第一行。

+1

謝謝,試過了,但輸出 –

+0

沒有變化工作,謝謝 –