【发布时间】:2015-09-14 19:06:31
【问题描述】:
我在我的Activity 和自定义ActionBar 中使用ActionBar,每个Fragment 都有单独的布局文件。我在FrameLayout中添加了总共6个Fragments
MainActionBarActivity.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。
当我试图从一个片段移动到另一个片段时,应用程序在同一行 parent.setContentInsetsAbsolute 上再次崩溃,并指示 Null Pointer Exception。
【问题讨论】:
标签: android android-fragments android-actionbar android-fragmentactivity