简体   繁体   English

在Android Studio中使用自定义标题栏

[英]Using custom title bar in android studio

I have imported an eclipse project into android studio. 我已将eclipse项目导入android studio。 In android studio the project was running without any hitches. 在android studio中,项目正在顺利运行。 However when using android studio, the app crashes. 但是,当使用android studio时,应用程序崩溃。 This is caused by use of a custom title bar. 这是由于使用自定义标题栏引起的。 Here is my code: 这是我的代码:

public class WelcomeActivity extends ActionBarActivity implements
    OnClickListener {
TextView terms, proceed;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_welcome);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.startup_windows_title);

    terms = (TextView) findViewById(R.id.textViewTerms);
    proceed = (TextView) findViewById(R.id.textViewProceed);

    Intent i = new Intent(this, ChatHeadDrawerService.class);
    startService(i);

    terms.setOnClickListener(this);
    proceed.setOnClickListener(this);
}

And the logcat content: 和logcat的内容:

  01-05 12:28:31.457    6080-6080/com.gigavia.gigit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gigavia.gigit/com.gigavia.gigit.WelcomeActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1972)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
            at android.app.ActivityThread.access$600(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4440)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
            at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:210)
            at android.app.Activity.requestWindowFeature(Activity.java:3083)
            at com.gigavia.gigit.WelcomeActivity.onCreate(WelcomeActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1936)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
            at android.app.ActivityThread.access$600(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4440)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

The error is on this line 错误在这条线上

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

How do I overcome this? 我该如何克服呢?

Try anyone of it. 尝试任何一个。 will may helpful to you. 对您可能会有帮助。

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



@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.startup_windows_title);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);

 }

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

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