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
getActivity().getActionBar() is giving null on fragment
I think the best solution is make a call back from your fragment like below:
Create a call back
public interface OnActionBarListener {
void onChangeActionBarTitle(int score);
}
and implement it in your activity
public class YourActivity extends AppCompatActivity
implements OnActionBarListener {
@Override
public void onChangeActionBarTitle(int score) {
mActionBar.setTitle("Your new score is :"+ score);
}
}
and in your fragment
public class YourFragment extends Fragment {
OnActionBarListener mListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof YourActivity) {
mListener = (OnActionBarListener) context;
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mListener != null) mListener.onChangeActionBarTitle(i);
return rootView;
}
}
Hope this helps !
UPDATE 1: base on your request, if you want your activity listen every button click on your fragment, try below code in your fragment
Button mButton1;
Button mButton2;
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeTitle(your_score);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeTitle(your_score);
}
});
void changeTitle(int score) {
if (mListener != null) mListener.onChangeActionBarTitle(i);
}
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.
Android FragmentActivity returns null in getActionBar()
You must extend ActionBarActivity instead of FragmentActivity to have Actionbar with fragments.
If you're using the v7 appcompat library, your activity should instead extend ActionBarActivity, which is a subclass of FragmentActivity (for more information, read Adding the Action Bar).
Still you can try this,
ActionBar actionBar = (ActionBarActivity)getActivity().getSupportActionBar();
More detail you can found here.
http://developer.android.com/training/basics/fragments/creating.html
getActionBar() returning null
Your
BaseActivity
mustextends
fromActionBarActivity
and notActivity
public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
Use
getSupportActionBar();
to get the ActionBar
Related Topics
How to Parse the Androidmanifest.Xml File Inside an .Apk Package
Error Inflating Class Fragment
Httpclient Won't Import in Android Studio
Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)
Error "The Connection to Adb Is Down, and a Severe Error Has Occurred."
Adb Not Finding My Device/Phone (Macos X)
Recyclerview Expand/Collapse Items
Eclipse Error: R Cannot Be Resolved to a Variable
Sending Message Through Whatsapp
Push Notification Works Incorrectly When App Is on Background or Not Running
Android: How to Have Viewpager Wrap_Content
How to Detect Android App Uninstall
API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error
Android Writing Logs to Text File
Include .So Library in APK in Android Studio
Android App Out of Memory Issues - Tried Everything and Still at a Loss