错误:“在添加内容之前必须调用requestFeature()”,尽管在setContentView()之前调用它

时间:2012-05-31 18:45:07

标签: java android android-activity actionbarsherlock

我有一个使用ActionBarSherlock的Activity。现在我想在该操作栏中显示一个Indeterminate Progress,并按照ActionBarSherlock中的示例进行操作:

public class MainMenu extends SimpleWebActivity implements BackgroundBrowserReciever {
    private Boolean windowFeatureCalled;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);

        this.windowFeatureCalled = true;
        setContentView(R.layout.main_menu);
                ....
    }

SimpleWebActivity是一个扩展SherlockActivity的抽象类。

但是当我尝试加载该Activity时,我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dalthed.tucan/com.dalthed.tucan.ui.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

为什么会出现此错误,即使我在调用setContentView之前请求WindowFeature?

2 个答案:

答案 0 :(得分:5)

在你的Super.onCreate之后移动它,这应该可以解决你的问题。

     @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

//HERE
 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

            setContentView(R.layout.main_menu);
  this.windowFeatureCalled = true;
                    ....
        }

答案 1 :(得分:1)

让我们尝试一下这对我有用

@Override
    public void onCreate(Bundle savedInstanceState) {

//HERE
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_menu);

         this.windowFeatureCalled = true;
                ....
    }