findViewById 无法构建 WebView
findViewById failing to build WebView
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");
}
为什么会出现以下错误:
error: non-static method findViewById(int) cannot be referenced from a static context
where T is a type-variable:
T extends View declared in method findViewById(int)
我厌倦了浪费时间阅读 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>
使用这个
@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");
尝试更改此直播 = (WebView) WebView.findViewById(...)
和
livestream = (WebView) findViewById(...)
要使 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");
}
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");
}
为什么会出现以下错误:
error: non-static method findViewById(int) cannot be referenced from a static context where T is a type-variable: T extends View declared in method findViewById(int)
我厌倦了浪费时间阅读 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>
使用这个
@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");
尝试更改此直播 = (WebView) WebView.findViewById(...) 和 livestream = (WebView) findViewById(...)
要使 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");
}