【问题标题】:Not able to modify Cordova WebView无法修改 Cordova WebView
【发布时间】:2016-05-11 08:09:52
【问题描述】:

我正在使用 EmberJs 开发 Cordova 应用程序。我需要本地 ajax 调用,即对 file:/// url 的 ajax 调用。科尔多瓦不允许这样做。所以我发现我需要修改 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,那也将解决我的问题。 提前致谢。

【问题讨论】:

  • file:/// 怎么可能是 ajax 服务器?
  • 由于我的应用程序的离线功能,我无法将其放入服务器 :(

标签: android ajax cordova webview


【解决方案1】:

你得到 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);
}

【讨论】:

  • 非常感谢 :) 我对 Android 知之甚少。再次感谢您的回答。
猜你喜欢
  • 2017-11-28
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
相关资源
最近更新 更多