Android - Using AppCompat/ActionBar for older devices(java.lang.NullPointerException) -


I have an application that currently works on Android versions 4.x and more. I am trying to change it, so it can be used by older devices such as 2.x and more. Everything is working fine except for the action bar, whenever I am trying to achieve, every time a null pointer is getting the exception SupportActionBar The issue at this point is that a lot has been discussed in the forums but I Still can not solve it.

The application uses the AppCompat Library, but it does not seem to work on older devices.

Styles.xml file

  & lt; Style name = "appbashtheme" parent = "theme.my app" & gt; & Lt; Item name = "android: spinnerItemStyle" gt; @ Style / spinner item & lt; / Item & gt; & Lt; Item name = "android: spinnerDropDownItemStyle" gt; @ Style / spinner itam Dropdown item & lt; / Item & gt; & Lt; Item name = "android: windowNoTitle" & gt; True & lt; / Item & gt; & Lt; / Style & gt;  

MyApp.xml file

  & lt; Style name = "Theme.MyApp" generator = "@ style / theme.AppCompat" & gt; & Lt; Item name = "ActionBarTymebackground" & gt; @drawable / cable_backfold & lt; / Item & gt; & Lt; Item name = "popup menu style" & gt; @ Style / popup menu.may app & lt; / Item & gt; & Lt; Item name = "dropdown listwish" & gt; @ Style / dropdown list view MyPass & lt; / Item & gt; & Lt; Item name = "Actionbar tab style" & gt; @ Style / actionbarabet style.maypect & lt; / Item & gt; & Lt; Item name = "Action draft style" & gt; @ Style / dropdownname.map & lt; / Item & gt; & Lt; Item name = "Action bell style" & gt; @ Style / actionbar Transpier MyPass & lt; / Item & gt; & Lt; Item name = "Action modebackground" & gt; @drawable / cab_baband_top & lt; / Item & gt; & Lt; Item name = "Action ModeSplitbackground" & gt; @drawable / cab_bangfold_bottom & lt; / Item & gt; & Lt; Item name = "ActionMedClass Button" & gt; @ Style / action button Closed. MyPass & lt; / Item & gt;  

Attempt to receive activity ActionBar

  Protected Zero OnCreate (bundled 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 (incorrect) This.getActionBar () setDisplayShowCustomEnabled (true). This.getActionBar () setDisplayShowTitleEnabled (incorrect). LayoutInflatorInfliler = (Layout Inflator) this.getSystemService (References LAOUT_INFLATER_SERVICE); See V = inflator.inflate (R.layout.actionbar_title, null); . (CFTextView) v.findViewById (R.id.actionbarTitle)) setText (this.getTitle ()); // Assign this to see the actionbars. Gate actionbars () SetCustomView (v); } Else {// It does not support a supported action bar to call a private class within a supported class. Optionbarbar = new notActionBar (); Nab.getNotActionBar (); // This is an error line}  

The private class receiving the supported action bar because I can not expand the ActionBarActivity because Action fully expands the ActionBarActivity instead of receiving ActionBar directly A private class is used.

  Personal class notActionBar extends ActionBarActivity {Public Zero getNotActionBar (ActionBar ActionBar} = getSupportActionBar (); // NULL POINTER EXCEPTION Action bar.setIcon (R.drawable.gem); ActionBar.setDisplayHomeAsUpEnabled (incorrect); ActionBar.setDisplayOptions (ActionBar.DISPLAY_SHOW_CUSTOM); ActionBar.setDisplayShowCustomEnabled (true); This.getSupportActionBar () setDisplayShowCustomEnabled (true). This.getSupportActionBar () setDisplayShowTitleEnabled (wrong). LayoutInflatorInfliler = (Layout Inflator) this.getSystemService (References LAOUT_INFLATER_SERVICE); See V = inflator.inflate (R.layout.actionbar_title, null); . (CFTextView) v.findViewById (R.id.actionbarTitle)) setText (this.getTitle ()); // Assign this to view the actionbars .getSupportActionBar (). SetCustomView (v); } Public Zero Settlement ActionBase (string A) {getSupportActionBar () SetTitle (A); }}  

You should not try to illustrate such activities in Android :

notActionBar nab = new No Actionbar ();

The path is to start actions in Android with startActivity or startActivityForResult method.

But in any way, the solution should increase the problem in your main activity to ActionBarActivity instead of Fragment Activity .

You can even if you use pieces! Because ActionBarActivity increases FragmentActivity .

View


Comments