![](/img/trans.png)
[英]How to attach onClickListener of an item in custom actionBar in fragments
[英]NPE in custom actionbar for all fragments
我在Activity
和自定义ActionBar
使用ActionBar
,并为每个Fragment
使用单独的布局文件。 我在FrameLayout
中总共添加了6个Fragment
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
。
当我尝试从一个片段移动到另一个片段时,应用程序再次通过指示Null Pointer Exception
在同一行parent.setContentInsetsAbsolute
崩溃。
尝试使用
actionBar.setCustomView(mCustomView);
代替
actionBar.setCustomView(R.layout.actionbar_profile_view);
提供布局的ID,android将为您膨胀一个新视图,该视图不同于您膨胀并分配给mCustomView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.