【发布时间】: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