0

I'm using ActionBar in my Activity and custom ActionBar with separate layout file for each Fragment. There are total 6 Fragments that I'm adding in FrameLayout

MainActionBarActivity.java

public class MainActionBarActivity extends ActionBarActivity implements OnHeadlineSelectedListener,UserToProfileFragmentInterface {

// code here...

}

Similarly, one of the fragment is ProfileFragment.java

public class ProfileFragment extends Fragment   {

    Context context;
    public final String TAG = "ProfileFragment";
    FrameLayout fragmentContainer;
    private int mCurrentlyShowingFragment;
    public final String SAVED_STATE_KEY = ProfileFragment.class.getSimpleName();

    public static ProfileFragment newInstance(Fragment.SavedState savedState) {
        ProfileFragment frag = new ProfileFragment();
        frag.setInitialSavedState(savedState);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.temp_profile, container, false);
        context = getActivity();
        fragmentContainer = (FrameLayout)view.findViewById(R.id.flayout);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState == null) {
            getChildFragmentManager().beginTransaction().add(R.id.flayout,ProfileFragmentDetails.newInstance()).addToBackStack("").commit();
          //  setRetainInstance(true);
            mCurrentlyShowingFragment = 0;
        } else {
            mCurrentlyShowingFragment = savedInstanceState.getInt("currently_showing_fragment");
        }

    }

That is only replacing new Fragment - ProfileFragmentDetails.

Now in this Fragment I'm trying to use custom ActionBar for that i write code in onResume():

@Override
public void onResume(){

    Log.d(TAG, "onResume;");
    super.onResume();
    Log.d(TAG, "mSingleTon.profileUpdated:" + mSingleTon.profileUpdated);
    if(getActivity().getSupportFragmentManager().getBackStackEntryCount()>0){
        ActionBar actionBar =  ((MainActionBarActivity) getActivity()).getSupportActionBar();

        ((MainActionBarActivity) getActivity()).invalidateOptionsMenu();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        View mCustomView = LayoutInflater.from(context).inflate(R.layout.actionbar_profile_view, null);
        actionBar.setCustomView(R.layout.actionbar_profile_view);
        actionBar.setDisplayShowCustomEnabled(true);

        Toolbar parent =(Toolbar) mCustomView.getParent();//first get parent toolbar of current action bar
        if(parent != null)
        {
            parent.setContentInsetsAbsolute(0,0);// set padding programmatically to 0dp
        }
        View v1 = actionBar.getCustomView();
        imageViewBroadcastBack = (ImageView)v1.findViewById(R.id.imageViewBroadcastBack);
        imageViewProfileSetting = (ImageView)v1.findViewById(R.id.imageViewProfileSetting);
        textViewScreenTitle = (TextView)v1.findViewById(R.id.textViewScreenTitle);
        textViewScreenTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "HelveticaBold.ttf"));

        textViewScreenTitle.setText("Profile");
    }
    else
    {
        //getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        //userProfileResponse = null;
        /*
        // Clear all back stack.
        int backStackCount = getActivity().getSupportFragmentManager().getBackStackEntryCount();
        for (int i = 0; i < backStackCount; i++) {

            // Get the back stack fragment id.
            int backStackId = getActivity().getSupportFragmentManager().getBackStackEntryAt(i).getId();

            getActivity().getSupportFragmentManager().popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        }
        // end of for
        */
        this.uid = mSingleTon.userResponse.getUser().getUid();


        ActionBar actionBar =  ((MainActionBarActivity) getActivity()).getSupportActionBar();
        ((MainActionBarActivity) getActivity()).invalidateOptionsMenu();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        View mCustomView = LayoutInflater.from(context).inflate(R.layout.actionbar_profile_view, null);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT);
        actionBar.setCustomView(R.layout.actionbar_profile_view);
        actionBar.setDisplayShowCustomEnabled(true);
         Toolbar parent = (Toolbar) mCustomView.getParent();
        if(parent != null)
        {
            parent.setContentInsetsAbsolute(0, 0);
        }

        //actionBar.setCustomView(R.layout.actionbar_profile_view);

        View v1 = actionBar.getCustomView();
        imageViewBroadcastBack = (ImageView)v1.findViewById(R.id.imageViewBroadcastBack);
        textViewScreenTitle = (TextView)v1.findViewById(R.id.textViewScreenTitle);
        textViewScreenTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "HelveticaBold.ttf"));
        imageViewProfileSetting = (ImageView)v1.findViewById(R.id.imageViewProfileSetting);
    }

Its giving me null on this line parent.setContentInsetsAbsolute if i put inside if condition check, then it does not crash the app, but it not sets the width to fillparent.

When I'm trying to move from one fragment to another then again app crash on the same line parent.setContentInsetsAbsolute by indicating Null Pointer Exception.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Erum
  • 790
  • 3
  • 14
  • 36

1 Answers1

0

try using

actionBar.setCustomView(mCustomView);

instead of

actionBar.setCustomView(R.layout.actionbar_profile_view);

providing the layout's id, android will inflate a new view for you which is different from the one you inflated and assigned to mCustomView

Blackbelt
  • 156,034
  • 29
  • 297
  • 305