删除标题栏时出错

时间:2013-10-30 08:56:22

标签: java android eclipse android-actionbar android-titlebar

请查看以下代码

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class Home extends Activity {

    private TextView textView23;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         //Removing the title bar
         this.requestWindowFeature(Window.FEATURE_NO_TITLE);


         setContentView(R.layout.activity_home);

        //Setting up the Action Bar
         ActionBar actionBar = getActionBar();  //to support lower version too
         actionBar.setDisplayShowCustomEnabled(true);
         View customView=getLayoutInflater().inflate(R.layout.window_title, null);
         actionBar.setCustomView(customView);
         actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));



        textView23 = (TextView)findViewById(R.id.textView23);
        textView23.setSelected(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

}

我在这里删除应用程序的标题栏。但是,只要执行此代码,我就会收到以下错误。

10-30 08:09:58.480: E/AndroidRuntime(2747): FATAL EXCEPTION: main
10-30 08:09:58.480: E/AndroidRuntime(2747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xx/com.xx.xx.Home}: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:137)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at dalvik.system.NativeStart.main(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747): Caused by: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.xx.xx.Home.onCreate(Home.java:29)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Activity.performCreate(Activity.java:5104)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-30 08:09:58.480: E/AndroidRuntime(2747):     ... 11 more

我正在尝试这样做,因为以下输出

enter image description here

正如您所看到的,应用程序标题和我的操作栏都会显示出来。我不想要应用程序标题或标题图片。

3 个答案:

答案 0 :(得分:2)

如果没有标题,则无法使用ActionBar。所以删除行

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

您的主要目的是从标题栏中删除标题和图标,然后使用下面的代码

ActionBar actionBar = getActionBar();  //to support lower version too
actionBar.setDisplayShowHomeEnabled(false); // Remove icon
actionBar.setDisplayShowTitleEnabled(false); // Remove title text
actionBar.setDisplayShowCustomEnabled(true);
View customView=getLayoutInflater().inflate(R.layout.window_title, null);
actionBar.setCustomView(customView);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

答案 1 :(得分:0)

试试这个:this.requestWindowFeature(getWindow().FEATURE_NO_TITLE); 这对我有用。

还试试这个: 在AndroidManifest <activity>标记中,添加:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

答案 2 :(得分:0)

在Manifest中,您可以使用主题android:theme="@android:style/Theme.NoTitleBar.Fullscreen"作为<activity>代码

将此getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);放入您的java类中。

这将删除默认的应用名称和图标。

然后您可以使用ActionBar设置应用名称。