SherlockNavigationDrawer实现问题

时间:2014-02-05 17:21:00

标签: android actionbarsherlock navigation-drawer android-navigation

我正在尝试实施Sherlock导航抽屉

GITHUB LINK

进入我的项目,但我有一些问题。解决了大部分问题之后,现在我在启动活动时遇到了非法状态异常。我不明白为什么。

02-05 15:09:32.994: E/AndroidRuntime(28194): FATAL EXCEPTION: main
02-05 15:09:32.994: E/AndroidRuntime(28194): Process: com.uy.cocoapp, PID: 28194
02-05 15:09:32.994: E/AndroidRuntime(28194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uy.cocoapp/com.uy.cocoapp.DrawerActivity}: java.lang.IllegalStateException: MainFragment must be attached to a SherlockFragmentActivity.
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.os.Looper.loop(Looper.java:136)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread.main(ActivityThread.java:5102)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at java.lang.reflect.Method.invoke(Method.java:515)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at dalvik.system.NativeStart.main(Native Method)
02-05 15:09:32.994: E/AndroidRuntime(28194): Caused by: java.lang.IllegalStateException: MainFragment must be attached to a SherlockFragmentActivity.
02-05 15:09:32.994: E/AndroidRuntime(28194):    at com.actionbarsherlock.app.SherlockFragment.onAttach(SherlockFragment.java:25)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.Activity.performStart(Activity.java:5258)
02-05 15:09:32.994: E/AndroidRuntime(28194):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
02-05 15:09:32.994: E/AndroidRuntime(28194):    ... 11 more

从GitHub中提供的示例中,我更改了布局,替换了框架的textview

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <FrameLayout
        android:id="@+id/drawer_content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView android:id="@+id/left_drawer"
              android:layout_width="300dp"
              android:layout_height="match_parent"
              android:layout_gravity="left"
              android:background="@android:color/white"/>
</android.support.v4.widget.DrawerLayout>

这是MainFragment类,我也修改了所以我可以处理多个片段(我从制表符改为此,这就是为什么它们被称为TabFragment)

public class MainFragment extends SherlockFragment {

    TabFragment1 fragment1;
    TabFragment2 fragment2;
    TabFragment3 fragment3;

    private DrawerLayout mDrawerLayout;
    private ListView listView;
    //private TextView mContent;

    private ActionBarHelper mActionBar;

    private SherlockActionBarDrawerToggle mDrawerToggle;

    public static Fragment newInstance() {
        Fragment f = new MainFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fragment1 = new TabFragment1();
        fragment2 = new TabFragment2();
        fragment3 = new TabFragment3();

        setRetainInstance(true);
        setHasOptionsMenu(true);
    }

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

        mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
        listView = (ListView) view.findViewById(R.id.left_drawer);
        //mContent = (TextView) view.findViewById(R.id.content_text);

        mDrawerLayout.setDrawerListener(new DemoDrawerListener());
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        String[] TITLES = {
                "Tu Codigo",   
                "Locales",
                "Mapa",       
                "FAQ",
                "Terms & Cond",
                "Cerrar Sesion"
        };

        listView.setAdapter(new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, TITLES));
        listView.setOnItemClickListener(new DrawerItemClickListener());
        listView.setCacheColorHint(0);
        listView.setScrollingCacheEnabled(false);
        listView.setScrollContainer(false);
        listView.setFastScrollEnabled(true);
        listView.setSmoothScrollbarEnabled(true);

        mActionBar = createActionBarHelper();
        mActionBar.init();

        // ActionBarDrawerToggle provides convenient helpers for tying together
        // the
        // prescribed interactions between a top-level sliding drawer and the
        // action bar.
        mDrawerToggle = new SherlockActionBarDrawerToggle(this.getActivity(), mDrawerLayout, R.drawable.ic_drawer_light, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater = ((SherlockFragmentActivity)getActivity()).getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*
         * The action bar home/up action should open or close the drawer.
         * mDrawerToggle will take care of this.
         */
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * This list item click listener implements very simple view switching by
     * changing the primary content text. The drawer is closed when a selection
     * is made.
     */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //hacer switch con position e ir cambiando

            selectItem(position);

            mDrawerLayout.closeDrawer(listView);
        }
    }

    private void selectItem(int position) {

        FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.drawer_content_frame, fragment1, "TabFragment1");
            ft.addToBackStack("TabFragment1");
            break;
        case 1:
            ft.replace(R.id.drawer_content_frame, fragment2, "TabFragment2");
            ft.addToBackStack("TabFragment2");
            break;
        case 2:
            ft.replace(R.id.drawer_content_frame, fragment3, "TabFragment3");
            ft.addToBackStack("TabFragment3");
            break;
        }
        ft.commit();
        mDrawerLayout.closeDrawer(listView);
    }


    /**
     * A drawer listener can be used to respond to drawer events such as
     * becoming fully opened or closed. You should always prefer to perform
     * expensive operations such as drastic relayout when no animation is
     * currently in progress, either before or after the drawer animates.
     * 
     * When using ActionBarDrawerToggle, all DrawerLayout listener methods
     * should be forwarded if the ActionBarDrawerToggle is not used as the
     * DrawerLayout listener directly.
     */
    private class DemoDrawerListener implements DrawerLayout.DrawerListener {
        @Override
        public void onDrawerOpened(View drawerView) {
            mDrawerToggle.onDrawerOpened(drawerView);
            mActionBar.onDrawerOpened();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            mDrawerToggle.onDrawerClosed(drawerView);
            mActionBar.onDrawerClosed();
        }


        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            mDrawerToggle.onDrawerStateChanged(newState);
        }
    }

    /**
     * Create a compatible helper that will manipulate the action bar if
     * available.
     */
    private ActionBarHelper createActionBarHelper() {
        return new ActionBarHelper();
    }



    private class ActionBarHelper {
        private final ActionBar mActionBar;
        private CharSequence mDrawerTitle;
        private CharSequence mTitle;

        private ActionBarHelper() {
            mActionBar = ((SherlockFragmentActivity)getActivity()).getSupportActionBar();
        }

        public void init() {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setHomeButtonEnabled(true);
            mTitle = mDrawerTitle = getActivity().getTitle();
        }

        /**
         * When the drawer is closed we restore the action bar state reflecting
         * the specific contents in view.
         */
        public void onDrawerClosed() {
            mActionBar.setTitle(mTitle);
        }

        /**
         * When the drawer is open we set the action bar to a generic title. The
         * action bar should only contain data relevant at the top level of the
         * nav hierarchy represented by the drawer, as the rest of your content
         * will be dimmed down and non-interactive.
         */
        public void onDrawerOpened() {
            mActionBar.setTitle(mDrawerTitle);
        }

        public void setTitle(CharSequence title) {
            mTitle = title;
        }
    }

}

由于

1 个答案:

答案 0 :(得分:1)

logcat说:

  

引起:java.lang.IllegalStateException:MainFragment必须附加到SherlockFragmentActivity。

即,您的活动必须延长SherlockFragmentActivity