2014-02-19 20 views
1

我有一个应用程序,目前在Android版本4.x和更多。 我想改变它,以便可以使用较旧的设备,如2.x和更多。 除了操作栏之外,一切正常。每次我尝试getSupportActionBar时,我都会收到一个空指针异常。这是在论坛上讨论过的很多问题,但我仍然无法解决。Android - 在较老的设备上使用AppCompat/ActionBar(java.lang.NullPointerException)

该应用程序使用appCompat库,但它似乎不适用于较旧的设备。

styles.xml文件

<style name="AppBaseTheme" parent="Theme.MyApp"> 
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item> 
    <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

MyApp.xml文件

<style name="Theme.MyApp" parent="@style/Theme.AppCompat"> 
     <item name="actionBarItemBackground">@drawable/selectable_background</item> 
     <item name="popupMenuStyle">@style/PopupMenu.MyApp</item> 
     <item name="dropDownListViewStyle">@style/DropDownListView.MyApp</item> 
     <item name="actionBarTabStyle">@style/ActionBarTabStyle.MyApp</item> 
     <item name="actionDropDownStyle">@style/DropDownNav.MyApp</item> 
     <item name="actionBarStyle">@style/ActionBar.Transparent.MyApp</item> 
     <item name="actionModeBackground">@drawable/cab_background_top</item> 
     <item name="actionModeSplitBackground">@drawable/cab_background_bottom</item> 
     <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.MyApp</item> 

活动试图让动作条

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_auction_list); 



    if (android.os.Build.VERSION.SDK_INT >=11) 
    { 
     getActionBar().setIcon(R.drawable.gem); 
     getActionBar().setDisplayHomeAsUpEnabled(false); 
     this.getActionBar().setDisplayShowCustomEnabled(true); 
     this.getActionBar().setDisplayShowTitleEnabled(false); 

     LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.actionbar_title, null); 

     ((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle()); 

     //assign the view to the actionbar 
     this.getActionBar().setCustomView(v); 
    } 
    else 
    { 
//This calls a private class inside the same class to get a supported action bar 
      notActionBar nab = new notActionBar(); 
      nab.getNotActionBar(); //THIS IS THE ERROR LINE 


} 

私有类是可以获得支持的行为离子棒。我使用了私有类而不是直接获取操作栏,因为我无法扩展ActionBarActivity,因为活动全部就绪扩展了片段。

private class notActionBar extends ActionBarActivity { 
    public void getNotActionBar(){ 
     ActionBar actionBar = getSupportActionBar(); //NULL POINTER EXCEPTION 
     actionBar.setIcon(R.drawable.gem); 
     actionBar.setDisplayHomeAsUpEnabled(false); 
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     actionBar.setDisplayShowCustomEnabled(true); 
     this.getSupportActionBar().setDisplayShowCustomEnabled(true); 
     this.getSupportActionBar().setDisplayShowTitleEnabled(false); 

     LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflator.inflate(R.layout.actionbar_title, null); 

     ((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle()); 

     //assign the view to the actionbar 
     this.getSupportActionBar().setCustomView(v); 

} 

    public void setTitleActionBar(String a){ 

     getSupportActionBar().setTitle(a); 


    } 
    } 

回答

0

你不应该试图实例化的Android这样的活动:

notActionBar nab = new notActionBar(); 

的方式开始活动Android是与startActivity(Intent intent)或startActivityForResult方法。

但无论如何,您的问题的解决方案应该扩展ActionBarActivity而不是FragmentActivity在您的主要活动。

可以如果你使用碎片,也可以这样做!因为ActionBarActivity extends FragmentActivity。他们的

看到related answer here

+0

非常感谢。因为我认为Action bar活动没有扩展Fragments,所以我在做内部类的所有工作。它工作正常,现在我正在从oncreate方法获取操作栏。 – user3328051

+0

酷!我很高兴,这对你有效:-) – donfuxx

0

你也应该考虑使用仅getSupportActionBar()或getActionBar()的任何一个。由于您希望定位较早的版本,因此只能使用getSupportActionBar()并从您的代码中移除所有关于getActionBar()的引用。

Action Bar的完整指南是here.

+0

感谢您的信息!我认为支持只对旧版本有用!你救了我一些代码行! – user3328051

相关问题