【问题标题】:Android native Web View not working flutterAndroid本机Web View无法正常工作
【发布时间】:2019-11-12 09:17:24
【问题描述】:

我正在构建一个颤振应用程序。我想从 android native 打开 webview。我正在使用方法通道调用网络。但是当调用 webview 它显示错误 Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference

代码如下所示

main.dart

new RaisedButton(onPressed: ()async{
    final response=await channel.invokeMethod("WebView","");
        getRes(response);
    },
child:new Text("Go to Web View"))

而java代码是

MainActivity.java

public class MainActivity extends FlutterActivity {
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
    new MethodCallHandler() {
        @Override

            if (methodCall.method.equals("WebView")){
                Log.d("LOG","Entered in WebView ");
                webview.loadUrl("https://www.journaldev.com"); // Error is showing on here
            }
    });

        GeneratedPluginRegistrant.registerWith(this);
        webview2 = (WebView) findViewById(R.id.web_book1);

         }
    }

错误显示在webview.loadUrl("https://www.journaldev.com"); 行中

web_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/web_book1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

如何在 Flutter 视图中打开 android 原生 WebView。怎么可能?

【问题讨论】:

    标签: android flutter dart flutter-layout


    【解决方案1】:

    如果你想在 Flutter 应用的小部件树中加载内联 WebView,或者如果你想打开应用内浏览器,你可以试试我的插件flutter_inappbrowser编辑:它已重命名为flutter_inappwebview)。

    下面给出了一个示例(参见完整示例here):

    ...
    
    child: InAppWebView(
      initialUrl: "https://flutter.dev/",
      initialHeaders: {},
      initialOptions: InAppWebViewWidgetOptions(
          inAppWebViewOptions: InAppWebViewOptions(
            debuggingEnabled: true,
          )
      ),
      onWebViewCreated: (InAppWebViewController controller) {
        webView = controller;
      },
      onLoadStart: (InAppWebViewController controller, String url) {
    
      },
      onLoadStop: (InAppWebViewController controller, String url) {
    
      },
    ),
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2016-10-16
      • 2013-07-02
      • 1970-01-01
      • 2017-03-16
      相关资源
      最近更新 更多