2013-01-21 83 views
0

为什么getActionBar()返回null?自定义窗口标题和ActionBar

我在我的应用程序中有自定义窗口标题。 下面我AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.map" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <permission 
     android:name="com.example.map.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="com.example.map.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/customTheme" > 
     <activity 
      android:name="com.example.map.MainActivity" 
      android:configChanges="orientation|screenSize" 
      android:screenOrientation="portrait" 
      android:windowSoftInputMode="adjustResize" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.map.MapActivity" 
      android:screenOrientation="portrait" 
      android:label="@string/app_name" > 
     </activity> 
     <activity 
      android:name="com.example.map.PersonalInformationActivity" 
      android:screenOrientation="portrait" 
      android:label="@string/app_name" > 
     </activity> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="MY_KEY" /> 
    </application> 
</manifest> 

问题与MainActivity.It可与动作条标签+片段(下面的代码)。在其他活动中一切正常。

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.activity_main); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_header); 

     final ActionBar actionBar = getActionBar(); 

     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     String label1 = getResources().getString(R.string.label1); 
     Tab tab = actionBar.newTab(); 
     tab.setText(label1); 
     TabListener<Login> tl = new TabListener<Login>(this, label1, Login.class); 
     tab.setTabListener(tl); 
     actionBar.addTab(tab); 

     String label2 = getResources().getString(R.string.label2); 
     tab = actionBar.newTab(); 
     tab.setText(label2); 
     TabListener<Registration> tl2 = new TabListener<Registration>(this, label2, Registration.class); 
     tab.setTabListener(tl2); 
     actionBar.addTab(tab); 

    } 

    private class TabListener<T extends Fragment> implements ActionBar.TabListener { 
     private Fragment mFragment; 
     private final Activity mActivity; 
     private final String mTag; 
     private final Class<T> mClass; 

     public TabListener(Activity activity, String tag, Class<T> clz) { 
      mActivity = activity; 
      mTag = tag; 
      mClass = clz; 
     } 

     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      // Check if the fragment is already initialized 
      if (mFragment == null) { 
       // If not, instantiate and add it to the activity 
       mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
       ft.add(android.R.id.content, mFragment, mTag); 
      } else { 
       // If it exists, simply attach it in order to show it 
       ft.attach(mFragment); 
      } 
     } 

     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      if (mFragment != null) { 
       // Detach the fragment, because another one is being attached 
       ft.detach(mFragment); 
      } 
     } 

     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // User selected the already selected tab. Usually do nothing. 

      } 
     } 
    } 
} 

回答

0

getActionBar()在API级别11.在你的清单中介绍,您要求您的应用程序将在API级别8上运行。它不会。

Here有很多原因的呼叫可以在设备上返回null与API> = 11

+0

,但还是同样的问题。我认为片段和窗口自定义标题之间存在冲突。 – tohafrit

+0

@tohafrit将minSdkVersion更改为11当然不会改变该调用的行为。如果您在

1

如果你想使用ActionBar您需要申请的ActionBar 请参见下面的代码。

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

    // The Action Bar is a window feature. The feature must be requested 
    // before setting a content view. 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

    setContentView(R.layout.main); 

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 
+0

好吧,requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)和getWindow()。setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_header)怎么样?我需要为Tab添加ActionBar并为Activity添加自定义标题。 – tohafrit

+0

@tohafrit您是否阅读过我在答复中提供链接的讨论?只是问问。 –

+0

是的,我读了讨论。 – tohafrit

0

我有类似的问题,我曾经得到下面的线路错误,因为 我设置的内容视图之前请求自定义标题功能。

setContentView(R.layout.userlayout); 
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 

第一个请求的功能和问题走了

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 
setContentView(R.layout.userlayout); 

希望这是我从8改成11,你想要的解决方案..