无法修改Cordova WebView

时间:2016-05-11 08:09:53

标签: android ajax cordova webview

我正在与EmberJs一起开发Cordova应用程序。在那里我需要本地ajax调用,即ajax调用file:/// url。科尔多瓦不允许这样做。所以我发现我需要修改Cordova WebView设置。为此,我尝试了这段代码:

import android.os.Build;
import android.os.Bundle;
import org.apache.cordova.*;
import org.apache.cordova.engine.SystemWebView;
import android.webkit.WebSettings;

public class MainActivity extends CordovaActivity
{
    SystemWebView webView = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        webView  = (SystemWebView) appView.getView();
        WebSettings settings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            settings.setAllowFileAccessFromFileURLs(true);
            settings.setAllowUniversalAccessFromFileURLs(true);
        }
        loadUrl(launchUrl);
    }
}

但我总是得到以下错误:

Runtime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scb.prototype/com.scb.prototype.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View org.apache.cordova.CordovaWebView.getView()' on a null object reference
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:148)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                      Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View org.apache.cordova.CordovaWebView.getView()' on a null object reference
                                                         at com.scb.prototype.MainActivity.onCreate(MainActivity.java:37)
                                                         at android.app.Activity.performCreate(Activity.java:6237)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:148) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

appView返回null。对此有何解决方案? 如果有人可以建议如何在Cordova中的file:/// url中允许ajax,那么这也将解决我的问题。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您收到NullPointerException,因为未调用super.init()且您的appView未初始化。

SystemWebView webView = null;

{{1}}