所有片段的自定义操作栏中的NPE

时间:2015-09-14 19:06:31

标签: android android-fragments android-actionbar android-fragmentactivity

我在ActionBar和自定义Activity中使用ActionBar,并为每个Fragment使用单独的布局文件。我在Fragment

中添加了总共6 FrameLayoutMainActionBarActivity.java

public class MainActionBarActivity extends ActionBarActivity implements OnHeadlineSelectedListener,UserToProfileFragmentInterface { // code here... }

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

Fragment

这只是替换新的ProfileFragmentDetails - Fragment

现在正在ActionBar我尝试使用自定义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); }

null

如果我进入parent.setContentInsetsAbsolute条件检查,它会在if这一行给我fillparent,然后它不会使应用程序崩溃,但它不会将宽度设置为{{1} }。

当我尝试从一个片段移动到另一个片段时,再次通过指示parent.setContentInsetsAbsolute在同一行Null Pointer Exception上应用崩溃。

1 个答案:

答案 0 :(得分:0)

尝试使用

actionBar.setCustomView(mCustomView);

而不是

actionBar.setCustomView(R.layout.actionbar_profile_view);

提供布局的ID,android会为您提供一个新的视图,该视图与您充气并分配给mCustomView的视图不同