Android本机Web视图无法正常工作

问题描述 投票:0回答:1

我正在开发一个Flutter应用程序。我想从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>

如何在抖动视图中打开android native WebView。怎么可能?

我正在开发一个Flutter应用程序。我想从android native打开webview。我正在使用方法通道调用网络。但是当调用webview时,显示错误尝试调用虚拟方法'...

android flutter dart flutter-layout
1个回答
1
投票

[如果要在Flutter应用的小部件树中加载内联WebView,或者要打开应用内浏览器,则可以尝试使用我的插件flutter_inappbrowserEDIT

© www.soinside.com 2019 - 2024. All rights reserved.