尝试在片段中使用SlidingPaneLayout时的nullpointerexpection(onCreateView)

时间:2014-06-28 23:56:06

标签: android nullpointerexception fragment slidingpanelayout

在尝试使用SlidingPaneLayout时,我在片段的onCreateView()中获得了一个nullpointer异常。

mSlidingLayout.setPanelSlideListener(new SliderListener());

如果我删除了这个SlidingPaneLayout的使用,只是在片段中显示某些东西的Textview就行了。

下面是onCreateView代码以及LogCat错误输出。

public class FragmentwithSlidingPane extends Fragment {

ListView lv_addblockparameters;

private SlidingPaneLayout mSlidingLayout;
private ListView mList;

private ActionBarHelper mActionBar;

//Constructor
public FragmentwithSlidingPane() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_testlayout, container, false);

    /*
     * Following code from:
     * http://androidtrainningcenter.blogspot.com/2013/06/slidingpanelayout-android-making.html
     */

    mSlidingLayout = (SlidingPaneLayout) rootView.findViewById(R.id.sliding_pane_layout);
    mList = (ListView) rootView.findViewById(R.id.left_pane);

            /********************************
            * LINE BELOW IS WHERE I GET NULLPOINTEREXCEPTION
            * This line is FragmentwithSlidingPane.java:71 stated in error trace below  
            *********************************/
    mSlidingLayout.setPanelSlideListener(new SliderListener());
    mSlidingLayout.openPane();

    //Populating the Listview
    String List[] = new String[5];
    List[0] = "Apps >>";
    List[1] ="URLs >>";
    List[2] ="Notifications >>";
    List[3] ="Texts >>";
    List[4] ="Calls >>";

    mList.setAdapter(new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, List));
    mList.setOnItemClickListener(new ListItemClickListener());

    return rootView;
}

}

/**
 * This panel slide listener updates the action bar accordingly for each
 * panel state.
 */
private class SliderListener extends
        SlidingPaneLayout.SimplePanelSlideListener {
    @Override
    public void onPanelOpened(View panel) {
        mActionBar.onPanelOpened();
    }

    @Override
    public void onPanelClosed(View panel) {
        mActionBar.onPanelClosed();
    }
}

以下是我的LogCat错误输出。

06-28 23:45:31.322: E/AndroidRuntime(4294): FATAL EXCEPTION: main
06-28 23:45:31.322: E/AndroidRuntime(4294): Process: com.mavdev.focusoutfacebook, PID: 4294
06-28 23:45:31.322: E/AndroidRuntime(4294): java.lang.NullPointerException
06-28 23:45:31.322: E/AndroidRuntime(4294):     at com.mavdev.focusoutfacebook.fragments.FragmentwithSlidingPane.onCreateView(FragmentwithSlidingPane.java:71)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.os.Handler.handleCallback(Handler.java:733)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.os.Looper.loop(Looper.java:136)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at java.lang.reflect.Method.invoke(Method.java:515)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-28 23:45:31.322: E/AndroidRuntime(4294):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我认为你选择了错误的xml文件我的意思是:fragment_testlayout