简体   繁体   English

如何修复无法解决Android中的方法'setContentView'

[英]How to Fix cannot resolve method 'setContentView' in Android

I'm trying to write an Android App and all seems to work except for one line ie 'setContentView' 我正在尝试编写一个Android应用程序,并且似乎所有内容都可以正常工作,只有一行,例如“ setContentView”

Is there a way I could substitute the above function 有什么办法可以代替上面的功能

This is the code in question and the problem is in line 18 这是有问题的代码,问题在第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);
    }



}

} }

All the app is to do is to have a bottom navigation bar and bring up a loading screen and then display a webpage in a webview 该应用程序要做的就是拥有一个底部的导航栏并弹出一个加载屏幕,然后在webview中显示一个网页

Could someone please help. 有人可以帮忙。 Thank you in advance 先感谢您

The class you have made is a Fragment , not an Activity , but your onCreate method looks like it was copied from an Activity. 您创建的类是Fragment ,不是Activity ,但是您的onCreate方法看起来像是从Activity复制的。 There is no setContentView method in a Fragment. 片段中没有setContentView方法。

You would set the layout in onCreateView and set up other things in, say, onActivityCreated or onCreateView itself. 您可以在onCreateView设置布局,并在onActivityCreatedonCreateView本身中设置其他内容。 For example: 例如:

@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;
}

Take a look at the Android guide for Fragments here , or use an Activity instead. 此处查看Fragments的Android指南,或改用Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM