android无法解析方法setcontentview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23034301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:42:09  来源:igfitidea点击:

android cannot resolve method setcontentview

androidandroid-fragments

提问by kilomo

I came across an error today in android studio. I am trying to create a about us screen in the app. The layout xml file has been created. Any help is appreciated. Thanks.

我今天在 android studio 中遇到了一个错误。我正在尝试在应用程序中创建一个关于我们的屏幕。已创建布局 xml 文件。任何帮助表示赞赏。谢谢。

error: cannot resolve method setcontentview(int)

错误:无法解析方法 setcontentview(int)

package example.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class AboutFragment extends android.app.Fragment {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about_screen);
}

}

回答by Raghunandan

Your class extends Fragmentand you have setContentView(R.layout.about_screen);. setContentViewis a method of Activity class.

你的课程扩展了Fragment,你有setContentView(R.layout.about_screen);. setContentView是Activity类的一个方法。

Instead inflate about_screen.xmlin onCreateViewof Fragment

相反,膨胀about_screen.xmlonCreateViewFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.about_screeen, container, false);
    return rootView;
}