我正在尝试将 SlidingMenu 包含在片段中。我之所以想在片段中放置一个 SlidingMenu 是因为我的 viewPager 的一个页面将显示它。只是对我如何扩展片段感到困惑,因为其中一个示例扩展了SlidingFragmentActivity
。
public class Activity A extends SlidingFragmentActivity {}
我一直在尝试这样,
public class ResponsiveUIActivityE extends SherlockFragment{
View rootView;
private Fragment mContent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SlidingFragmentActivity2 sl = new SlidingFragmentActivity2();
getActivity().setTitle("R.string.responsive_ui");
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rootView = vi.inflate(R.layout.two_fragments, null, false);
// check if the content frame contains the menu frame
if (rootView.findViewById(R.layout.two_fragments) == null) {
//sl.setBehindContentView(R.layout.two_fragments);
sl.getSlidingMenu().setSlidingEnabled(true);
sl.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// show home as up so we can toggle
} else {
}
// set the Above View Fragment
if (savedInstanceState != null){
mContent = getChildFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new BirdGridFragment(0);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent)
.commit();
// set the Behind View Fragment
getChildFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new BirdMenuFragment())
.commit();
// customize thse SlidingMenu
SlidingMenu sm = sl.getSlidingMenu();
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindScrollScale(0.25f);
sm.setFadeDegree(0.25f);
}
return rootView;
}
}
滑动片段活动2
public class SlidingFragmentActivity2 extends SherlockFragmentActivity implements SlidingActivityBase {
private SlidingActivityHelper mHelper;
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHelper = new SlidingActivityHelper(this);
mHelper.onCreate(savedInstanceState);
}
/* (non-Javadoc)
* @see android.app.Activity#onPostCreate(android.os.Bundle)
*/
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mHelper.onPostCreate(savedInstanceState);
}
/* (non-Javadoc)
* @see android.app.Activity#findViewById(int)
*/
@Override
public View findViewById(int id) {
View v = super.findViewById(id);
if (v != null)
return v;
return mHelper.findViewById(id);
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mHelper.onSaveInstanceState(outState);
}
/* (non-Javadoc)
* @see android.app.Activity#setContentView(int)
*/
@Override
public void setContentView(int id) {
setContentView(getLayoutInflater().inflate(id, null));
}
/* (non-Javadoc)
* @see android.app.Activity#setContentView(android.view.View)
*/
@Override
public void setContentView(View v) {
setContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
/* (non-Javadoc)
* @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
@Override
public void setContentView(View v, LayoutParams params) {
super.setContentView(v, params);
mHelper.registerAboveContentView(v, params);
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#setBehindContentView(int)
*/
public void setBehindContentView(int id) {
setBehindContentView(getLayoutInflater().inflate(id, null));
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#setBehindContentView(android.view.View)
*/
public void setBehindContentView(View v) {
setBehindContentView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#setBehindContentView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
public void setBehindContentView(View v, LayoutParams params) {
mHelper.setBehindContentView(v, params);
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#getSlidingMenu()
*/
public SlidingMenu getSlidingMenu() {
return mHelper.getSlidingMenu();
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#toggle()
*/
public void toggle() {
mHelper.toggle();
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#showAbove()
*/
public void showContent() {
mHelper.showContent();
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#showBehind()
*/
public void showMenu() {
mHelper.showMenu();
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#showSecondaryMenu()
*/
public void showSecondaryMenu() {
mHelper.showSecondaryMenu();
}
/* (non-Javadoc)
* @see com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivityBase#setSlidingActionBarEnabled(boolean)
*/
public void setSlidingActionBarEnabled(boolean b) {
mHelper.setSlidingActionBarEnabled(b);
}
/* (non-Javadoc)
* @see android.app.Activity#onKeyUp(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean b = mHelper.onKeyUp(keyCode, event);
if (b) return b;
return super.onKeyUp(keyCode, event);
}
}
日志猫:
08-07 15:23:13.943: E/AndroidRuntime(10612): FATAL EXCEPTION: main
08-07 15:23:13.943: E/AndroidRuntime(10612): java.lang.NullPointerException
08-07 15:23:13.943: E/AndroidRuntime(10612): at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity2.getSlidingMenu(SlidingFragmentActivity2.java:101)
08-07 15:23:13.943: E/AndroidRuntime(10612): at com.actionbarsherlock.sample.styled.ResponsiveUIActivityE.onCreateView(ResponsiveUIActivityE.java:41)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.View.measure(View.java:15181)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.View.measure(View.java:15181)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.View.measure(View.java:15181)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.View.measure(View.java:15181)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-07 15:23:13.943: E/AndroidRuntime(10612): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.View.measure(View.java:15181)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.Choreographer.doFrame(Choreographer.java:525)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.os.Handler.handleCallback(Handler.java:615)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.os.Looper.loop(Looper.java:137)
08-07 15:23:13.943: E/AndroidRuntime(10612): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-07 15:23:13.943: E/AndroidRuntime(10612): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 15:23:13.943: E/AndroidRuntime(10612): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 15:23:13.943: E/AndroidRuntime(10612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-07 15:23:13.943: E/AndroidRuntime(10612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 15:23:13.943: E/AndroidRuntime(10612): at dalvik.system.NativeStart.main(Native Method)
08-07 15:28:13.978: I/Process(10612): Sending signal. PID: 10612 SIG: 9