我需要在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)为空。获取CordovaWebview是否需要更改。
答案 0 :(得分:-1)
也许这会有所帮助:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you need to call init() so the appView member gets initialized...
super.init();
appView.loadUrl("https://google.com");
}