public class FeedFragment extends Fragment {
private WebView livestream;
public FeedFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_feed, container, false);
livestream = (WebView) WebView.findViewById(R.id.livestream);
livestream.loadUrl("http://www.google.com");
}
为什么会出现以下错误:
错误:无法从静态上下文引用非静态方法findViewById(int) 其中T是类型变量: T扩展了在方法findViewById(int)中声明的View
我厌倦了浪费时间阅读android文档并提出错误消息!
片段xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#ffb067"
tools:context=".FeedFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:text="Feed Fragment" />
<WebView
android:id="@+id/livestream"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 0 :(得分:1)
使用此
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
livestream = (WebView) view.findViewById(R.id.livestream);
livestream.loadUrl("http://www.google.com");
}
并从onCreateView删除这些行:
livestream = (WebView) WebView.findViewById(R.id.livestream);
livestream.loadUrl("http://www.google.com");
答案 1 :(得分:0)
尝试更改此直播=(WebView)WebView.findViewById(...) 用 livestream =(WebView)findViewById(...)
答案 2 :(得分:0)
要内嵌WebView加载,请使用以下代码
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
livestream = (WebView) view.findViewById(R.id.livestream);
livestream.setWebViewClient(new WebViewClient());
livestream.loadUrl("http://www.google.com");
}