安卓无法解析setcontentview方法

7
我今天在Android Studio中遇到了一个错误。我正在尝试在应用程序中创建一个“关于我们”屏幕。布局XML文件已经创建好了。如果有帮助的话,请告诉我。谢谢。
错误:无法解决方法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);
}

}

2
在Fragments中使用onCreateView(),而在Activity中使用setContentView(R.layout.about_screen)。 - amit singh
1个回答

16

你的类扩展了 Fragment,并且你调用了 setContentView(R.layout.about_screen); 方法。 setContentView 是 Activity 类的一个方法。

相反地,在 FragmentonCreateView 方法中解析并加载布局文件 about_screen.xml

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接