getActionBar() returns Null (AppCompat-v7 21)
You need to call getSupportActionBar()
on an ActionBarActivity
. Do not call getActionBar()
-- that is not available on older devices, and for the new r21 edition of appcompat-v7
, I would expect it to return null
all the time, as the new ActionBarActivity
disables and replaces the system action bar.
getActionBar() returns null in PreferenceActivity (AppCompat-v7 21)
as xXx requested I am providing example how I done:
public class SettingsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
// use action bar here
ActionBar actionBar = getSupportActionBar();
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
addPreferencesFromResource(R.xml.pref_settings);
}
}
}
getActionBar() returns NULL in actionBar
I always have this error....
I found a solution which I put this to my Style
it will be work and getActionBar()
won't be null anymore...
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
Hope this help you
getActionBar returns null
It seems you need to request having an Actionbar (!= titlebar) either via a Theme or via below code.
@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. Normally this is set automatically
// by your Activity's theme in your manifest. The provided system
// theme Theme.WithActionBar enables this for you. Use it as you would
// use Theme.NoTitleBar. You can add an Action Bar to your own themes
// by adding the element <item name="android:windowActionBar">true</item>
// to your style definition.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main);
// experiment with the ActionBar
ActionBar actionBar = getActionBar();
actionBar.hide();
}
Code from [here]
Android ActionBar always null
*First of all include this dependency in your gradle file
compile 'com.android.support:appcompat-v7:22.2.0'
*You should extend your activity to AppCompactActivity and start using toolbar instead of ActionBar.
*This is how you use Toolbar in your MainActivity
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
*this is how to add toolbar in your activity_main.xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
*your theme should look something like this styles.xml
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
make sure you add this style name in your manifest for your application.
*It will be better if you use v4.app.Fragment rather than using Fragment.
*this is how you should set title from your Fragment class
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.home);
Refer this clear Tutorial which will surely help you setting up material Navigation drawer.
If you want to quickly start off without much pain then you can try integrating this third party library.
ActionBar object is returned as null
You need to use getSupportActionBar()
when you use AppCompatActivity
instead of getActionBar()
.
EDIT
When you use AppCompat
theme you have to use AppCompatActivity
.
Example code how to get ActionBar
:
Activity activity = getActivity();
if(activity != null && activity instanceof AppCompatActivity) {
AppCompatAcitivyt appCompatActivity = (AppCompatActivity) activity;
ActionBar actionBar = appCompatActivity.getSupportActionBar();
//your code
}
getActionBar() returns null in api 19, targets api 21 min sdk 16
If you want to use ActionBar
on support library, you have to extend ActionBarActivity
class instead of Activity
, and use getSupportActionBar()
in place of getActionBar()
to get ActionBar
.
getActionBar(), getSupportActionBar() returns java.nullpointer.exception
From what i can get from your question is that you are looking to design custom Action Bar for your Application,right?
If yes then below code may help you out:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowCustomEnabled(true);
bar.setCustomView(R.layout.row);
bar.show();
}
}
Please Note that enabling customenabled property on action bar is important for your Actionbar to implement custom view.
bar.setDisplayShowCustomEnabled(true)
And also no need to use to LayoutInflator you could directly feed action bar custom layout by using
actionbar.setCustomView(your layout file)
Related Topics
Android Studio: Creating Landscape Layouts
Image Size for All Screen Devices
Broadcastreceiver for Location
Calling Hidden API in Android to Turn Screen Off
Gmail 5.0 App Fails with "Permission Denied for the Attachment" When It Receives Action_Send Intent
How to Write a Drawable Resource to a File
What's the Maximum Size for an Android Shared Preference Value
Fast Video Compression on Android
Full Text Search Example in Android
How to Retrieve Key Alias and Key Password for Signed APK in Android Studio(Migrated from Eclipse)
How to Write a Custom Filter for Listview with Arrayadapter
How to Save Image in Shared Preference in Android | Shared Preference Issue in Android with Image
How to Disable Status Bar Click and Pull Down in Android
Parse JSON Array Response Using Retrofit & Gson
Android: Programmatically Adding Buttons to a Layout
Android Recyclerview Findviewholderforadapterposition Returns Null