2016-07-27 10 views
0

Ich habe andere ähnliche Probleme hier und auf github untersucht, aber es ist mir nicht gelungen, eine auf Analogie basierende Lösung zu finden.(Nativescript) Kann JavaScript-Objekt mit ID x bei Index 0 nicht konvertieren Schnittstellenimplementierung

Ich versuche, die folgende Plugin zu bekommen auf Android zu arbeiten:

var app = require("application"); 
var context = android.content.Context; 
var TextToSpeech = android.speech.tts.TextToSpeech; 
var initialised = false; 
var tts; 

var text_to_speech = { 
    speak : function(text, queue, pitch, speakRate, volume){ 

     if(!tts || !initialised) { 
      tts = new TextToSpeech(context, new TextToSpeech.OnInitListener({ 
       onInit : function(status) { 
        // some code here 
       } 
      })); 
     } 
    } 
}; 

Leider eine Ausnahme erhalte ich, wenn das tts Objekt initialisiert wird:

JS: ORIGINAL STACKTRACE: 
JS: Error: Cannot convert JavaScript object with id 683715827 at index 0 
JS:  at Error (native) 
JS:  at Object.text_to_speech.speak (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/nativescript-texttospeech/texttospeech.js:20:10) 
JS:  at TextToSpeech.speakText (/data/data/org.nativescript.CzystyDywan/files/app/pages/texttospeech/texttospeech.component.js:9:13) 
JS:  at DebugAppView._View_TextToSpeech0._handle_tap_4_0 (TextToSpeech.template.js:148:28) 
JS:  at Object.<anonymous> (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/@angular/core/src/linker/view.js:316:24) 
JS:  at ZoneDelegate.invoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:281:29) 
JS:  at Object.NgZoneImpl.inner.inner.fork.onInvoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/@angular/core/src/zone/ng_zone_impl.js:45:41) 
JS:  at ZoneDelegate.invoke (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:280:35) 
JS:  at Zone.runGuarded (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:188:48) 
JS:  at Object.callback (/data/data/org.nativescript.CzystyDywan/files/app/tns_modules/zone.js/dist/zone-node.js:164:30) 
JS: ERROR CONTEXT: 
JS: [object Object] 
W/System.err(3701): at com.tns.Runtime.callJSMethodNative(Native Method) 
W/System.err(3701): at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:861) 
W/System.err(3701): at com.tns.Runtime.callJSMethodImpl(Runtime.java:726) 
W/System.err(3701): at com.tns.Runtime.callJSMethod(Runtime.java:712) 
W/System.err(3701): at com.tns.Runtime.callJSMethod(Runtime.java:693) 
W/System.err(3701): at com.tns.Runtime.callJSMethod(Runtime.java:683) 

Ich verstehe, dass dies hat etwas mit der Implementierung der TextToSpeech.OnInitListener-Schnittstelle zu tun, aber ich weiß wirklich nicht, was ich hier falsch mache. Hat jemand ähnliche Probleme gehabt und kann ein paar Tipps geben? Vielen Dank!

Antwort

0

Vergesst, ich konstruierte tatsächlich das TTS-Objekt falsch - anstatt app.android.context zu verwenden, benutzte ich android.content.Context, um den Kontext für den Konstruktor zu erhalten. Dies liegt daran, dass ich für die erste "undefiniert" wurde. Jetzt verwende ich diese Hilfsfunktion, um den aktuellen Kontext zu erhalten:

function _getContext() { 
    if (appModule.android.context) { 
     return (appModule.android.context); 
    } 
    var ctx = java.lang.Class.forName("android.app.AppGlobals").getMethod("getInitialApplication", null).invoke(null, null); 
    if (ctx) return ctx; 

    ctx = java.lang.Class.forName("android.app.ActivityThread").getMethod("currentApplication", null).invoke(null, null); 
    return ctx; 
}