2

我正在尝试在片段类中使用 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   
4

1 回答 1

3

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

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

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

于 2013-04-18T23:46:11.223 回答