Java 方法声明无效;返回片段中所需的WebView类型
我学习android,我想在Fragment中创建WebView。这是我写的Java 方法声明无效;返回片段中所需的WebView类型,java,android,webview,android-webview,Java,Android,Webview,Android Webview,我学习android,我想在Fragment中创建WebView。这是我写的 package app.com.youtubeapiv3.fragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import and
package app.com.youtubeapiv3.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import app.com.youtubeapiv3.R;
/**
* A simple {@link Fragment} subclass.
*/
public class WebView extends Fragment {
public WebView() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_web_view, container, false);
WebView mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
return v;
}
}
我收到的消息错误如下
错误:方法声明无效;返回类型必需错误:
期望
方法loeadUrl()和getSettings();无法解决。我不知道我该怎么办
XML
问题似乎出在您的findviewbyid
方法中。在fragment\u web\u视图
中检查网络视图的id是否为webview
?如果这不能解决问题,发布您的fragment\u web\u视图
xml它就不起作用了。我添加了我的fragment\u web\u view.xml问题是WebView
与片段冲突(因为它们都有相同的名称WebView
),我的建议是将片段重命名为类似MyCustomWebView
的其他名称。问题似乎出在findviewbyid
方法中。在fragment\u web\u视图
中检查网络视图的id是否为webview
?如果这不能解决问题,发布您的fragment\u web\u视图
xml它就不起作用了。我添加了我的fragment\u web\u view.xml问题是WebView
与片段冲突(因为它们都有相同的名称WebView
),我的建议是将片段重命名为类似MyCustomWebView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.WebView">
<!-- TODO: Update blank fragment layout -->
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</RelativeLayout>