我正在尝试编写一个Android应用程序,并且似乎所有内容都可以正常工作,只有一行,即“ setContentView”
有没有办法替代上述功能
这是有问题的代码,问题在第 18
行中public class HiraFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_hira,null);
}
WebView web;
ProgressBar progressBar;
ImageView logo;
ImageView bg;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) getView().findViewById(R.id.webView);
progressBar = (ProgressBar) getView().findViewById(R.id.progressBar);
logo = (ImageView) getView().findViewById(R.id.logo);
bg = (ImageView) getView().findViewById(R.id.bg);
text = (TextView) getView().findViewById(R.id.text);
web.setWebViewClient(new hira());
web.loadUrl("http://example.com/");
web.getSettings().setJavaScriptEnabled(true);
}
public class hira extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
logo.setVisibility(View.GONE);
bg.setVisibility(View.GONE);
text.setVisibility(View.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
logo.setVisibility(view.VISIBLE);
bg.setVisibility(view.VISIBLE);
text.setVisibility(view.VISIBLE);
}
}
}
该应用程序要做的就是拥有一个底部导航栏并弹出一个加载屏幕,然后在Webview中显示网页
请有人帮忙。 预先谢谢你
答案 0 :(得分:2)
您创建的类是Fragment
,而不是Activity
,但是您的onCreate
方法看起来像是从Activity复制的。片段中没有setContentView
方法。
您将在onCreateView
中设置布局,并在onActivityCreated
或onCreateView
本身中设置其他内容。例如:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hira, container, false);
// To access the views, you need to prefix them with the rootView like this
web = (WebView) rootView.findViewById(R.id.webView);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
logo = (ImageView) rootView.findViewById(R.id.logo);
bg = (ImageView) rootView.findViewById(R.id.bg);
text = (TextView) rootView.findViewById(R.id.text);
web.setWebViewClient(new hira());
web.loadUrl("http://example.com/");
web.getSettings().setJavaScriptEnabled(true);
return rootView;
}
看看Android片段here指南,或者改用Activity。