Problem when viewing web pages in QML using android.webkit.WebView
-
I have checked in several forums and I have found this:
The "stale local reference" error means that you're saving a local reference to some Java object between JNI calls;
You need to convert that reference to a global reference using the NewGlobalRef method before doing anything that would cause the reference to persist outside the scope of the one JNI call.
@jclass jc = env->FindClass(callbacks.name);
// Since Android ICS, class references are not global so we need to peg a
// global reference to the jclass returned by FindClass(), otherwise we get
// following error in the log:
// "JNI ERROR (app bug): attempt to use stale local reference 0xHHHHHHHH".
callbacks._class = static_cast<jclass>(env->NewGlobalRef(jc));@In you case try to convert that reference to a global reference.....
@jclass jc = env->FindClass(callbacks.name);
callbacks._class = static_cast<jclass>(env->NewGlobalRef(jc));@in any case I think that the error is the following block:
@void AndroidWebView::loadURL(QString url)
{
qDebug() << "AndroidWebView::loadURL" << url;
QAndroidJniObject qObjUrlString = QAndroidJniObject::fromString(url);
// The crash happens when I call this method
qObjWebView.callObjectMethod("loadUrl", "(Ljava/lang/String;)V", qObjUrlString.object<jstring>());
}@because loadUrl is a method of QDesktopServices class and I don't see this class in your code.
your code will be the following:
@void AndroidWebView::loadURL(QString url)
{
qDebug() << "AndroidWebView::loadURL" << url;QAndroidJniObject javaClass("QDesktopServices");
QAndroidJniEnvironment env;
jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
objectClass.callObjectMethod("loadUrl", "(Ljava/lang/String;)V", qObjUrlString.object<jstring>());
}@
-
Hi,
There's now the "QtWebView module":https://qt.gitorious.org/qt/qtwebview/source/a2dd3fb028d731b3971e442db81b693c98849900 that can be used on android
-
[quote author="nologinma" date="1408629120"]
You need to convert that reference to a global reference using the NewGlobalRef method before doing anything that would cause the reference to persist outside the scope of the one JNI call.
[/quote]
Hi! Thanks for help. I tried to call NewGlobalRef for all jobjects and jclasses I using in my code, but I still get the same error.[quote author="nologinma" date="1408629120"]
because loadUrl is a method of QDesktopServices class and I don't see this class in your code.
[/quote]
But I think I don't need to call any methods of QDesktopServices class. loadUrl, in this case, is the method of android.webkit.WebView class and described "here":http://developer.android.com/reference/android/webkit/WebView.html#loadUrl(java.lang.String).[quote author="SGaist" date="1408630724"]Hi,
There's now the "QtWebView module":https://qt.gitorious.org/qt/qtwebview/source/a2dd3fb028d731b3971e442db81b693c98849900 that can be used on android[/quote]
Hi. Thanks, this looks very closely to my needs. But I don't sure how I should build this? Can I build it with Qt5.3? -
Using qmake, AFAIK yes
-
[quote author="SGaist" date="1408723811"]Using qmake, AFAIK yes[/quote]
Got these results:
Alexeys-MacBook-Pro:qtwebview alexeykolikov$ ~/Qt5.3.1/5.3/android_armv7/bin/qmake
Alexeys-MacBook-Pro:qtwebview alexeykolikov$ make
cd src/ && ( test -e Makefile || /Users/alexeykolikov/Qt5.3.1/5.3/android_armv7/bin/qmake /Users/alexeykolikov/Documents/github/qtwebview/src/src.pro -o Makefile ) && make -f Makefile
cd webview/ && ( test -e Makefile || /Users/alexeykolikov/Qt5.3.1/5.3/android_armv7/bin/qmake /Users/alexeykolikov/Documents/github/qtwebview/src/webview/webview.pro -o Makefile ) && make -f Makefile
Project WARNING: You should probably load(qt_build_config) first in webview.pro for QtWebView, as the latter also load()s qt_module.
Project MESSAGE: Not doing so may lead to qt_module.prf overriding compiler/linker options in your .pro file.
Project MESSAGE: Ignore this warning with CONFIG+=no_qt_module_warning if you know what you are doing.
Project ERROR: Module does not define version.
make[1]: *** [sub-webview-make_first] Error 3
make: *** [sub-src-make_first] Error 2I guess it's not supposed to be built in this way. Looks like I should build it as a new Qt module like described "here":http://qt-project.org/wiki/Creating-a-new-module-or-tool-for-Qt#55af8d3b581bc7a96ac2a08eb19da92c. Please correct me if I'm wrong.
-
In my app I have used the following called in c++
@QAndroidJniObject m_url = QAndroidJniObject::fromString("storage/sdcard0/gps/gps.kml");
QAndroidJniObject m_application_type = QAndroidJniObject::fromString("application/vnd.google-earth.kml+xml");
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/Chronometer/Next72Utility", "openUrl", "(Ljava/lang/String;Ljava/lang/String;)V",m_url.object<jstring>(),m_application_type.object<jstring>());@and the java class is the following:
@//
// Next72Utility.java
//
package org.qtproject.example.Chronometer;import android.content.Context;
import android.app.Activity;
import java.lang.Runnable;import android.content.Intent;
import java.io.File;
import android.net.Uri;public class Next72Utility extends org.qtproject.qt5.android.bindings.QtActivity
{public static Next72Utility m_istance;
public Next72Utility()
{
m_istance = this;
}public static void openUrl(final String m_url, final String m_application_type)
{
m_istance.runOnUiThread(new Runnable() {
public void run() {Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(m_url); intent.setDataAndType(Uri.fromFile(file), m_application_type); m_istance.startActivity(intent); } });
}
}
@I hope that solves your case.
-
Indeed, it seems that there are some details missing. Patch underway
-
I have write the steps to introduce the java code into qt project into follwing post: ? "[SOLVED] Qt on Android : How to Vibrate the device ?":https://qt-project.org/forums/viewreply/190435/
I hope that it is usefull here.....