无法修改 Cordova WebView

Abh*_*hek 0 ajax android webview cordova

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

\n\n
import android.os.Build;\nimport android.os.Bundle;\nimport org.apache.cordova.*;\nimport org.apache.cordova.engine.SystemWebView;\nimport android.webkit.WebSettings;\n\npublic class MainActivity extends CordovaActivity\n{\n    SystemWebView webView = null;\n\n    @Override\n    public void onCreate(Bundle savedInstanceState)\n    {\n        super.onCreate(savedInstanceState);\n        // Set by <content src="index.html" /> in config.xml\n        webView  = (SystemWebView) appView.getView();\n        WebSettings settings = webView.getSettings();\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {\n            settings.setAllowFileAccessFromFileURLs(true);\n            settings.setAllowUniversalAccessFromFileURLs(true);\n        }\n        loadUrl(launchUrl);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我总是收到以下错误:

\n\n
Runtime: FATAL EXCEPTION: main\njava.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\n                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)\n                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)\n                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)\n                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)\n                                                         at android.os.Handler.dispatchMessage(Handler.java:102)\n                                                         at android.os.Looper.loop(Looper.java:148)\n                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)\n                                                         at java.lang.reflect.Method.invoke(Native Method)\n                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)\n                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)\n                                                      Caused by: java.lang.NullPointerException: Attempt to invoke interface method \'android.view.View org.apache.cordova.CordovaWebView.getView()\' on a null object reference\n                                                         at com.scb.prototype.MainActivity.onCreate(MainActivity.java:37)\n                                                         at android.app.Activity.performCreate(Activity.java:6237)\n                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)\n                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)\n                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)\xc2\xa0\n                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)\xc2\xa0\n                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)\xc2\xa0\n                                                         at android.os.Handler.dispatchMessage(Handler.java:102)\xc2\xa0\n                                                         at android.os.Looper.loop(Looper.java:148)\xc2\xa0\n                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)\xc2\xa0\n                                                         at java.lang.reflect.Method.invoke(Native Method)\xc2\xa0\n                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)\xc2\xa0\n                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)\xc2\xa0\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

USK*_*ity 5

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

SystemWebView webView = null;

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