简体   繁体   English

如何在 Android 中正确隐藏活动标题

[英]How to properly hide Activity title in Android

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

crashes my app with the error出现错误使我的应用程序崩溃

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

I know I have to request window feature before setting the content view and that's what I'm doing.我知道在设置内容视图之前我必须请求窗口功能,这就是我正在做的。 Why is the error still there?为什么错误仍然存​​在?

My Acitivty extends AppCompatActivity and is declared in manifest like this:我的 Acitivty 扩展了 AppCompatActivity 并在清单中声明如下:

 <activity
            android:name=".activity.CameraActivity"
            android:label="@string/title_activity_camera"
            android:theme="@style/Theme.AppCompat.Light.Dialog"></activity>

EDIT full stack trace:编辑完整的堆栈跟踪:

Process: irisrecognition.example.com.irisrecognition, PID: 29756
 java.lang.RuntimeException: Unable to start activity ComponentInfo{irisrecognition.example.com.irisrecognition/irisrecognition.example.com.irisrecognition.activity.CameraActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
     at android.app.ActivityThread.access$900(ActivityThread.java:177)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:145)
     at android.app.ActivityThread.main(ActivityThread.java:5942)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:359)
     at android.app.Activity.requestWindowFeature(Activity.java:3785)
     at irisrecognition.example.com.irisrecognition.activity.CameraActivity.onCreate(CameraActivity.java:56)
     at android.app.Activity.performCreate(Activity.java:6289)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:145) 
     at android.app.ActivityThread.main(ActivityThread.java:5942) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

You must call requestFeature before super.onCreate.您必须在 super.onCreate 之前调用 requestFeature。 it's necessary to call requestFeature() before super.onCreate() replace your code with有必要在 super.onCreate() 将您的代码替换为之前调用 requestFeature()

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

call requestWindowFeature before super.onCreaterequestWindowFeature之前调用super.onCreate

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

you can try this:你可以试试这个:

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

I hope this will help you.我希望这能帮到您。

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

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