【发布时间】:2014-08-08 07:21:52
【问题描述】:
我需要在 CordovaWebview 中加载外部 URL。我有以下代码。
public class ActionBar extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CordovaWebView webview = new CordovaWebView(this);
Config.init(this);
webview.loadUrl("http://www.google.com");
}
}
应用程序在一段时间后崩溃。
日志详情:
08-08 12:42:44.490: E/AndroidRuntime(32712): FATAL EXCEPTION: main
08-08 12:42:44.490: E/AndroidRuntime(32712): Process: com.example.actionbar, PID: 32712
08-08 12:42:44.490: E/AndroidRuntime(32712): java.lang.NullPointerException
08-08 12:42:44.490: E/AndroidRuntime(32712): at org.apache.cordova.CordovaWebView.stopLoading(CordovaWebView.java:546)
08-08 12:42:44.490: E/AndroidRuntime(32712): at org.apache.cordova.CordovaWebView$2.run(CordovaWebView.java:468)
08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Handler.handleCallback(Handler.java:733)
08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Handler.dispatchMessage(Handler.java:95)
08-08 12:42:44.490: E/AndroidRuntime(32712): at android.os.Looper.loop(Looper.java:212)
08-08 12:42:44.490: E/AndroidRuntime(32712): at android.app.ActivityThread.main(ActivityThread.java:5151)
08-08 12:42:44.490: E/AndroidRuntime(32712): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 12:42:44.490: E/AndroidRuntime(32712): at java.lang.reflect.Method.invoke(Method.java:515)
08-08 12:42:44.490: E/AndroidRuntime(32712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
08-08 12:42:44.490: E/AndroidRuntime(32712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-08 12:42:44.490: E/AndroidRuntime(32712): at dalvik.system.NativeStart.main(Native Method)
编辑 1
public class GetCordovaWebview extends Activity implements CordovaInterface {
protected static Activity currentActivity;
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
// Android Activity Life-cycle events
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout layout = new FrameLayout(this);
CordovaWebView webview = new CordovaWebView(this);
Config.init(this);
layout.addView(webview);
setContentView(layout);
webview.loadurl("file:///android_asset/www/index.html");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (this.cordova_webview != null) {
this.cordova_webview
.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};");
this.cordova_webview.loadUrl("about:blank");
this.cordova_webview.removeAllViews();
cordova_webview.handleDestroy();
}
}
@Override
public Activity getActivity() {
return this;
}
@Override
public ExecutorService getThreadPool() {
return threadPool;
}
@Override
public Object onMessage(String message, Object obj) {
Log.d(TAG, message);
if (message.equalsIgnoreCase("exit")) {
super.finish();
}
return null;
}
@Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
Log.d(TAG, "setActivityResultCallback is unimplemented");
}
@Override
public void startActivityForResult(CordovaPlugin cordovaPlugin,
Intent intent, int resultCode) {
Log.d(TAG, "startActivityForResult is unimplemented");
}
}
尝试了上面的代码行。但是应用程序崩溃了。在调试 Cordova 3.5 源代码时,调用了 stopLoading() webview 方法,WebviewClient (viewClient.isCurrentlyLoading) 变量 (viewClient) 为 null。获取 CordovaWebview 是否需要进行任何更改。
【问题讨论】:
-
还有其他方法可以获取 CordovaWebview 并动态加载 URL 吗?
标签: android cordova android-webview