Android的新手,如果有任何错误,请原谅
用FiveFragment.java发行,其中 mWebview 变量用红色下划线标出,“无法解析符号mWebview”
frament_five.xml:
<?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="com.example.ekatechhp.pkmapplication.FiveFragment">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
FragmentFive.java:
@SuppressLint("ValidFragment")
public class FiveFragment extends Fragment {
public FiveFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout. fragment_five, container, false);
mWebview = (WebView) view.findViewById(R.id.webview);
mWebview.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return view;
}
}
答案 0 :(得分:1)
声明 WebView ;
@SuppressLint("ValidFragment")
public class FiveFragment extends Fragment {
WebView mWebview ;//declare here
public FiveFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view=inflater.inflate(R.layout. fragment_five, container, false);
mWebview = (WebView) view.findViewById(R.id.webview);
mWebview.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return view;
}
}
并确保您导入此
import android.webkit.WebView;
答案 1 :(得分:0)
这是因为您没有在片段中声明mWebview
。
添加WebView mWebview;
进行声明。
最终代码是-
@SuppressLint("ValidFragment")
public class FiveFragment extends Fragment {
WebView mWebview;
public FiveFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout. fragment_five, container, false);
mWebview = (WebView) view.findViewById(R.id.webview);
mWebview.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return view;
}
}