片段类中的滑动菜单

时间:2013-04-18 22:46:18

标签: android android-fragments slidingmenu

我正在尝试在片段类中使用jfeinstein10 SlidingMenu。

public class ConversionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_conversion, container, false);

        super.onCreate(savedInstanceState);

        SlidingMenu menu = new SlidingMenu(BaseActivity.context);
        menu.setMode(SlidingMenu.LEFT);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        menu.attachToActivity((Activity) BaseActivity.context, SlidingMenu.SLIDING_WINDOW);
        menu.setMenu(R.layout.list);

        ListView lv = (ListView) rootView.findViewById(R.id.listView1);
        String[] values = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext().getApplicationContext(),
              android.R.layout.simple_list_item_1, android.R.id.text1, values);
        lv.setAdapter(adapter);

        return rootView;
    }
}

和我的list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

我在lv.setAdapter(adapter);获得零点异常可能是什么原因造成的?

这甚至是使用jfeinstein10 SlidingMenu的正确方法吗?

04-18 19:15:36.345: D/myapp(19089): java.lang.Exception 
04-18 19:15:36.345: D/myapp(19089):     at com.example.app1.ConversionFragment.onCreateView(ConversionFragment.java:44)
04-18 19:15:36.345: D/myapp(19089):     at android.app.Fragment.performCreateView(Fragment.java:1695)
04-18 19:15:36.345: D/myapp(19089):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-18 19:15:36.345: D/myapp(19089):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-18 19:15:36.345: D/myapp(19089):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-18 19:15:36.345: D/myapp(19089):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-18 19:15:36.345: D/myapp(19089):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-18 19:15:36.345: D/myapp(19089):     at android.os.Handler.handleCallback(Handler.java:725)
04-18 19:15:36.345: D/myapp(19089):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 19:15:36.345: D/myapp(19089):     at android.os.Looper.loop(Looper.java:137)
04-18 19:15:36.345: D/myapp(19089):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-18 19:15:36.345: D/myapp(19089):     at java.lang.reflect.Method.invokeNative(Native Method)
04-18 19:15:36.345: D/myapp(19089):     at java.lang.reflect.Method.invoke(Method.java:511)
04-18 19:15:36.345: D/myapp(19089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 19:15:36.345: D/myapp(19089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 19:15:36.345: D/myapp(19089):     at dalvik.system.NativeStart.main(Native Method)
04-18 19:15:38.001: D/AndroidRuntime(19089): Shutting down VM   
04-18 19:15:38.001: W/dalvikvm(19089): threadid=1: thread exiting with uncaught exception (group=0x41e8b930)    
04-18 19:15:38.025: E/AndroidRuntime(19089): FATAL EXCEPTION: main  
04-18 19:15:38.025: E/AndroidRuntime(19089): java.lang.NullPointerException 
04-18 19:15:38.025: E/AndroidRuntime(19089):    at com.example.app1.ConversionFragment.onCreateView(ConversionFragment.java:45)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.Fragment.performCreateView(Fragment.java:1695)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.os.Handler.handleCallback(Handler.java:725)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.os.Looper.loop(Looper.java:137)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at java.lang.reflect.Method.invoke(Method.java:511)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 19:15:38.025: E/AndroidRuntime(19089):    at dalvik.system.NativeStart.main(Native Method)
04-18 19:15:38.040: W/ActivityManager(389):   Force finishing activity com.example.app1/.BaseFragment   

1 个答案:

答案 0 :(得分:3)

好像你无法正确引用滑动菜单左侧使用的ListView。问题是你要求错误的视图(rootView)找到你的列表,返回null引用。您应该自己膨胀菜单视图,然后在膨胀的视图上调用findViewById(),如下所示:

...
View left = inflater.inflate(R.layout.list,null,false);
...
menu.setMenu(left);
...
ListView lv = (ListView) left.findViewById(R.id.listView1);
...

我认为你的代码中还有另外几个错误。如果要使用片段内的滑动菜单,则应使用菜单作为onCreateView方法的返回值。出于同样的原因,您不应将菜单附加到整个活动,因此您应删除相关的代码行。