2013-10-30 30 views
0

請看看下面的代碼錯誤時,標題欄被刪除

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

正如你所看到的,應用程序標題和我的行動吧這個,兩者都得到顯示。我不想要應用標題或標題圖片。

回答

2

沒有標題,你不能使用動作條。所以刪除行

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)); 
+0

最受歡迎... –

0

試試這個:this.requestWindowFeature(getWindow().FEATURE_NO_TITLE); 這對我有用。

也試試這個: 在你AndroidManifest <activity>標籤中,添加:

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

清單中,你可以使用主題android:theme="@android:style/Theme.NoTitleBar.Fullscreen"<activity>標籤

把這個getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);放在你的java類中。

這將刪除默認的應用程序名稱和圖標。

然後,您可以使用ActionBar來設置應用程序名稱。