Getactionbar() Returns Null

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

  1. Your BaseActivity must extends from ActionBarActivity and not Activity

    public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

  2. Use getSupportActionBar(); to get the ActionBar



Related Topics



Leave a reply



Submit