我有一个应用程序,目前在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);
}
}
非常感谢。因为我认为Action bar活动没有扩展Fragments,所以我在做内部类的所有工作。它工作正常,现在我正在从oncreate方法获取操作栏。 – user3328051
酷!我很高兴,这对你有效:-) – donfuxx