如何在 XML 布局(或编程)中膨胀 XWalkView?
How does one inflate XWalkView in an XML layout (or programmatic)?
我在扩展包含 XWalkView 的 XML 布局时遇到运行时异常的严重问题...
堆栈跟踪:
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:233)
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
at org.xwalk.core.ReflectionHelper.loadClass(ReflectionHelper.java:199)
at org.xwalk.core.ReflectionHelper$ConstructorHelper.loadConstructor(ReflectionHelper.java:37)
at org.xwalk.core.ReflectionHelper.createInstance(ReflectionHelper.java:246)
at org.xwalk.core.XWalkView.<init>(XWalkView.java:169)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.bean.project.XWalkUIActivity.onCreate(XWalkUIActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/com_bean_project_content"
android:theme="@style/com_bean_project_black" >
<!-- bottom controls -->
<RelativeLayout
android:id="@+id/com_bean_project_bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<!-- some buttons, nothing to worry about -->
</RelativeLayout>
<!-- fills most of the parent view -->
<org.xwalk.core.XWalkView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/com_bean_project_webview"
android:layout_alignParentTop="true"
android:layout_above="@id/com_bean_project_bottom_layout"/>
</RelativeLayout>
我做过的事情:
1) 在 Android Studio 中,我正在使用库中的 armeabi-v7a
文件夹,并且 xwalk_core_library_java_app_part.jar
和 xwalk_core_library_java_library_part.jar
这两个都包含在依赖项中。我不知道我是否应该只使用其中一个罐子?我在任何文档中都找不到有关它的任何信息。
2) XWalkView 的 activity 有 android:hardwareAccelerated="true"
3) 在人行横道下载页面 (https://crosswalk-project.org/documentation/downloads.html) 我已经下载了 Android webview (ARM)
的 11.40.277.7 版本
4) 我还将 crosswalk 中的 "res" 文件夹合并到我的项目中,但这没有什么区别。
5) 我也尝试过编程 inflation,但是,它也只使用 new XWalkView(mParentActivity, mParentActivity);
(我知道,愚蠢的构造函数):
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:233)
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
at org.xwalk.core.ReflectionHelper.loadClass(ReflectionHelper.java:199)
at org.xwalk.core.ReflectionHelper$ConstructorHelper.loadConstructor(ReflectionHelper.java:37)
at org.xwalk.core.ReflectionHelper.createInstance(ReflectionHelper.java:246)
at org.xwalk.core.XWalkView.<init>(XWalkView.java:192)
为了获得灵感,我查看了 https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkView.html 但是,这对这个问题没有帮助。
经过一天的疯狂实验,我终于放弃了尝试存储库的本地副本。
相反,我按照以下说明使用 Maven:https://crosswalk-project.org/documentation/embedding_crosswalk/crosswalk_aar.html
该页面的底部是 link 使用 Maven 设置的示例应用程序 - 这很有用。
注意:要了解可用的库版本,请转到此处:https://download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/xwalk_core_library/
例如,您将拥有 compile 'org.xwalk:xwalk_core_library:12.41.296.5'
而不是 "beta" 依赖项
在使用 maven 进行设置后,我又恢复到 XML inflation。
然而,我当时面临
04-02 16:38:31.036 14165-14165/com.bean.project W/System.err﹕ Caused by: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
04-02 16:38:31.037 14165-14165/com.bean.project W/System.err﹕ at org.xwalk.core.internal.XWalkViewInternal.<init>(XWalkViewInternal.java:197)
04-02 16:38:31.037 14165-14165/com.bean.project W/System.err﹕ at org.xwalk.core.internal.XWalkViewBridge.<init>(XWalkViewBridge.java:46)
疯狂还在继续...
public XWalkViewInternal(Context context, AttributeSet attrs) {
super(convertContext(context), attrs);
checkThreadSafety();
mActivity = (Activity) context;
mContext = getContext();
init(mContext, attrs);
}
他们怎么可能认为这是个好主意??!! /脸掌
编辑: 为了以编程方式将 XWalkView 添加到布局中,我在 ID 为 FULLSCREEN_YOUTUBE_CONTAINER_ID
的 XML 中放置了一个占位符(空)RelativeLayout,然后在 activity 的 onCreate() 中,我做了:
mYoutubeVideoViewContainer = (RelativeLayout) mLayoutView.findViewById(FULLSCREEN_YOUTUBE_CONTAINER_ID);
mYoutubeWebView = new XWalkView(mParentActivity, mParentActivity);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mYoutubeVideoViewContainer.addView(mYoutubeWebView, relativeParams);
也许这会对你有所帮助:
起初我也尝试在我的应用程序中使用罐子。那没有用,我得到了和你一样的例外。
然后我偶然发现了 this sample app,它展示了如何使用人行横道。
然后我将构建复制到我的应用程序中,一切正常。
这是我在 gradle 文件中添加的内容:
在存储库部分我添加了一行(在 crashlytics.. 下方)
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven'}
maven { url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'}
}
在依赖项部分我还添加了一行 - 最后一行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'org.xwalk:xwalk_core_library:10.39.235.15'
}
这是 xml 的 activity:
<org.xwalk.core.XWalkView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/walk_view"
android:layout_width="fill_parent"
android:background="#000000"
android:layout_height="fill_parent"
/>
这就是我在 java 代码中使用它的方式:
private void setXView(){
mXview = (XWalkView)findViewById(R.id.walk_view);
mXview.load("http://www.cnn.com",null);
}
不要忘记删除应用程序中的 jar,否则如果您的依赖项中有 fileTree(dir: 'libs', include: ['*.jar']),您可能会遇到编译错误。
我在扩展包含 XWalkView 的 XML 布局时遇到运行时异常的严重问题...
堆栈跟踪:
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:233)
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
at org.xwalk.core.ReflectionHelper.loadClass(ReflectionHelper.java:199)
at org.xwalk.core.ReflectionHelper$ConstructorHelper.loadConstructor(ReflectionHelper.java:37)
at org.xwalk.core.ReflectionHelper.createInstance(ReflectionHelper.java:246)
at org.xwalk.core.XWalkView.<init>(XWalkView.java:169)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.bean.project.XWalkUIActivity.onCreate(XWalkUIActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/com_bean_project_content"
android:theme="@style/com_bean_project_black" >
<!-- bottom controls -->
<RelativeLayout
android:id="@+id/com_bean_project_bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<!-- some buttons, nothing to worry about -->
</RelativeLayout>
<!-- fills most of the parent view -->
<org.xwalk.core.XWalkView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/com_bean_project_webview"
android:layout_alignParentTop="true"
android:layout_above="@id/com_bean_project_bottom_layout"/>
</RelativeLayout>
我做过的事情:
1) 在 Android Studio 中,我正在使用库中的 armeabi-v7a
文件夹,并且 xwalk_core_library_java_app_part.jar
和 xwalk_core_library_java_library_part.jar
这两个都包含在依赖项中。我不知道我是否应该只使用其中一个罐子?我在任何文档中都找不到有关它的任何信息。
2) XWalkView 的 activity 有 android:hardwareAccelerated="true"
3) 在人行横道下载页面 (https://crosswalk-project.org/documentation/downloads.html) 我已经下载了 Android webview (ARM)
4) 我还将 crosswalk 中的 "res" 文件夹合并到我的项目中,但这没有什么区别。
5) 我也尝试过编程 inflation,但是,它也只使用 new XWalkView(mParentActivity, mParentActivity);
(我知道,愚蠢的构造函数):
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Use SharedXWalkView if you want to support shared mode
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:233)
at org.xwalk.core.ReflectionHelper.handleException(ReflectionHelper.java:237)
at org.xwalk.core.ReflectionHelper.init(ReflectionHelper.java:132)
at org.xwalk.core.ReflectionHelper.loadClass(ReflectionHelper.java:199)
at org.xwalk.core.ReflectionHelper$ConstructorHelper.loadConstructor(ReflectionHelper.java:37)
at org.xwalk.core.ReflectionHelper.createInstance(ReflectionHelper.java:246)
at org.xwalk.core.XWalkView.<init>(XWalkView.java:192)
为了获得灵感,我查看了 https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkView.html 但是,这对这个问题没有帮助。
经过一天的疯狂实验,我终于放弃了尝试存储库的本地副本。
相反,我按照以下说明使用 Maven:https://crosswalk-project.org/documentation/embedding_crosswalk/crosswalk_aar.html
该页面的底部是 link 使用 Maven 设置的示例应用程序 - 这很有用。
注意:要了解可用的库版本,请转到此处:https://download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/xwalk_core_library/
例如,您将拥有 compile 'org.xwalk:xwalk_core_library:12.41.296.5'
在使用 maven 进行设置后,我又恢复到 XML inflation。
然而,我当时面临
04-02 16:38:31.036 14165-14165/com.bean.project W/System.err﹕ Caused by: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
04-02 16:38:31.037 14165-14165/com.bean.project W/System.err﹕ at org.xwalk.core.internal.XWalkViewInternal.<init>(XWalkViewInternal.java:197)
04-02 16:38:31.037 14165-14165/com.bean.project W/System.err﹕ at org.xwalk.core.internal.XWalkViewBridge.<init>(XWalkViewBridge.java:46)
疯狂还在继续...
public XWalkViewInternal(Context context, AttributeSet attrs) {
super(convertContext(context), attrs);
checkThreadSafety();
mActivity = (Activity) context;
mContext = getContext();
init(mContext, attrs);
}
他们怎么可能认为这是个好主意??!! /脸掌
编辑: 为了以编程方式将 XWalkView 添加到布局中,我在 ID 为 FULLSCREEN_YOUTUBE_CONTAINER_ID
的 XML 中放置了一个占位符(空)RelativeLayout,然后在 activity 的 onCreate() 中,我做了:
mYoutubeVideoViewContainer = (RelativeLayout) mLayoutView.findViewById(FULLSCREEN_YOUTUBE_CONTAINER_ID);
mYoutubeWebView = new XWalkView(mParentActivity, mParentActivity);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mYoutubeVideoViewContainer.addView(mYoutubeWebView, relativeParams);
也许这会对你有所帮助:
起初我也尝试在我的应用程序中使用罐子。那没有用,我得到了和你一样的例外。
然后我偶然发现了 this sample app,它展示了如何使用人行横道。 然后我将构建复制到我的应用程序中,一切正常。 这是我在 gradle 文件中添加的内容: 在存储库部分我添加了一行(在 crashlytics.. 下方)
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven'}
maven { url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'}
}
在依赖项部分我还添加了一行 - 最后一行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'org.xwalk:xwalk_core_library:10.39.235.15'
}
这是 xml 的 activity:
<org.xwalk.core.XWalkView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/walk_view"
android:layout_width="fill_parent"
android:background="#000000"
android:layout_height="fill_parent"
/>
这就是我在 java 代码中使用它的方式:
private void setXView(){
mXview = (XWalkView)findViewById(R.id.walk_view);
mXview.load("http://www.cnn.com",null);
}
不要忘记删除应用程序中的 jar,否则如果您的依赖项中有 fileTree(dir: 'libs', include: ['*.jar']),您可能会遇到编译错误。