Nice programing

android.util.AndroidRuntimeException : 콘텐츠를 추가하기 전에 requestFeature ()를 호출해야합니다.

nicepro 2020. 10. 15. 21:42
반응형

android.util.AndroidRuntimeException : 콘텐츠를 추가하기 전에 requestFeature ()를 호출해야합니다.


android.util.AndroidRuntimeException: requestFeature() must be called before adding content오류가 발생합니다. 아래 코드에서 볼 수 있듯이 줄은 코드 줄 requestWindowFeature(Window.FEATURE_NO_TITLE);앞에옵니다 setContentView(R.layout.mainmenu);. 이 onCreate () 코드는 거의 모든 활동에서 동일한 형식이며 지금까지 문제가 없었습니다. ADT 22로 업데이트 한 이후로 모든 곳에서 임의의 오류가 많이 발생했습니다. 나는 많은 오류를 제거했으며 이것이 나의 최근 오류입니다.

이 오류를 수정하려면 어떻게해야합니까?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainmenu);

LogCat

05-31 04:20:43.121: E/AndroidRuntime(14559): FATAL EXCEPTION: main
05-31 04:20:43.121: E/AndroidRuntime(14559): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.bibletrivia.lite/matt.lyons.bibletrivia.lite.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Looper.loop(Looper.java:137)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at dalvik.system.NativeStart.main(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.requestWindowFeature(Activity.java:3244)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.performCreate(Activity.java:5104)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 04:20:43.121: E/AndroidRuntime(14559):    ... 11 more

나는 또한이 문제에 직면했지만 super.onCreate ()호출하기 전에 창 요청을 호출 하면 문제가 해결 되었으므로 마찬가지로 시도하십시오 ..

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}

이것이 당신을 도울 것입니다 ... :)


수정 됨 : Android의 새 버전에 대한 다른 가능한 솔루션

Android 4.0 이하에서 상태 표시 줄 숨기기

<application
    ...
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    ...
</application>

활동 테마 사용의 장점은 다음과 같습니다.

  • 프로그래밍 방식으로 플래그를 설정하는 것보다 유지 관리가 쉽고 오류 발생 가능성이 적습니다.
  • 시스템에는 앱의 주요 활동을 인스턴스화하기 전에 UI를 렌더링하는 데 필요한 정보가 있기 때문에 UI 전환이 더 원활 해집니다.

Android 버전이 Jellybean보다 낮습니다.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // If the Android version is lower than Jellybean, use this call to hide
    // the status bar.
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.activity_main);
}

Android 4.1 이상에서 상태 표시 줄 숨기기

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

다음 사항에 유의하십시오.

  • UI 플래그가 지워지면 (예 : 활동에서 멀리 이동하여), 막대를 다시 숨기려면 앱에서이를 재설정해야합니다. 앱이 그에 따라 응답 할 수 있도록 UI 가시성 변경을 수신하는 방법에 대한 논의는 UI 가시성 변경응답을 참조하십시오 .
  • Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
  • The method setSystemUiVisibility() only has an effect if the view you call it from is visible.
  • Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

I got that exception (android.util.AndroidRuntimeException: requestFeature() must be called before adding content) when using

requestWindowFeature(Window.FEATURE_NO_TITLE);

in an older device running Android 2.3.5 (Gingerbread). I am using the v7 support library.

The error was fixed when I changed it to use:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

(This comes after my super.onCreate call in the fix, too). See docs at https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)

So it may be more a case of a misleading error message than anything else.


Please check your class is extend from Activity or ActionBarActivity. If you are using ActionBarActivity please use Activity.


If you're using your activity as a Dialog (with Theme.Dialog), then make sure you extend Activity instead of ActionBarActivity (the default given to you with the Android Studio wizard). Then you can use

requestWindowFeature(Window.FEATURE_NO_TITLE);

or

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

AFTER the super.onCreate()...


I also ran into this error from a different workflow. I created a custom DialogFragment class and I created two @Override functions - onCreateView and onCreateDialog. My onCreateView function grabbed a custom layout for the fragment, and my onCreateDialog function created an AlertDialog.Builder.

This appeared to not work because the onCreateDialog is called before onCreateView. After I deleted onCreateView [by moving my custom view inflation into onCreateDialog, i encountered the error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I realized my difficulty came from trying to implement both overrides since I wanted to 1) use a layout for the main view of the dialog, and 2) use the Builder pre-defined Positive / Negative buttons. My solution was to create the positive / negative buttons in my custom dialog view, so i deleted my implementation of the Override onCreateDialog function.

Hope this helps someone in the future!

Here are some SO questions that helped me:


 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>

    </style>

just only set style like this no any coding side changing is required.


I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to remove the title:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Extend (Activity) instead of (ActionBarActivity)

example: public class Itemdetails extends Activity {....

then in the onCreate write:

super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Your_Activity);

i think the simplest way to do this is use this below code

  getActionBar().setTitle(null);

Please try the following options, at least one of these should work for you:

  1. If you are using a DialogFragment then try not overriding both methods onCreateDialog(..) and onCreateView(..) (This change worked for me)

  2. try supportRequestWindowFeature(Window.FEATURE_NO_TITLE)/requestWindowFeature(Window.FEATURE_NO_TITLE) before setContentView(..) in the activity and after super.onCreate(..);

  3. Try option 2 before super.onCreate(..);


Misplaced your line: super.onCreate(savedInstanceState);

use this Manner:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrscanner);

참고URL : https://stackoverflow.com/questions/16939814/android-util-androidruntimeexception-requestfeature-must-be-called-before-add

반응형