Paramita
Paramita

Reputation: 83

getActivity().getActionBar() is giving null on fragment

I have a fragment , where I need to change the title of ActionBar dynamically. But when I am using the following code, it is giving me NullPointerException. Here getActionBar() is returning null,my question is how can I get the reference of ActionBar in Fragment class. Appreciate your help in advance.

Fragment Class code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      ActionBar mActionBar = getActivity().getActionBar();
      mActionBar.setTitle("Your new score is :"+ i);
      return rootView;
}

Upvotes: 3

Views: 931

Answers (5)

steven smith
steven smith

Reputation: 1577

I had the same problem. I had to explicitly find the toolbar and set it. Just set actionbar didn't work. Also, in my case the toolbar was in google supplied code so finding it was ... interesting and I I had to make sure I had the right toolbar type. For me, the Kotlin code looked like:

  import androidx.appcompat.widget.Toolbar
  ...
  val toolbar = requireActivity()
      .findViewById<Toolbar>(R.id.toolbar)
  toolbar?.title = getString(R.string.search_deck,
            currentDeck.deckName)

Upvotes: 1

Paramita
Paramita

Reputation: 83

So the problem is solved now. What I did is, created an Interface, and gave a callback to my Activity class. Thanks to @GianhTran for suggesting the solution. It is working well.

Here is my code snippet:

My Custom Button class(UtilOnClickListener.java):

//Created an Interface

public interface OnActionBarListener {
  void onChangeActionBarTitle(int score);
 }

// Initialized it in the constructor of that class

public class UtilOnClickListener {

public UtilOnClickListener(View view) {

if (Calculator.getContext() instanceof MainActivity) {
mListener = (OnActionBarListener) Calculator.getContext();
}
}
}

And here is the MainActivity.java

public class MainActivity extends AppCompatActivity implements UtilOnClickListener.OnActionBarListener {

 @Override  
public void onChangeActionBarTitle(int score) {
getSupportActionBar().setTitle(Integer.toString(score));
}
}

Upvotes: 0

GianhTran
GianhTran

Reputation: 3711

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);
    }

Upvotes: 1

Satender Kumar
Satender Kumar

Reputation: 635

Check for the theme whether you have used action bar theme with this activity Also check if you are extending your activity your with AppCompatActivity then use getSupportActionBar()

Upvotes: 0

Gokul Nath KP
Gokul Nath KP

Reputation: 15574

If you are using support library, try using getSupportActionBar() instead of getActionBar().

Upvotes: 0

Related Questions