illeglStateException:必须将VideoListFragment附加到SherlockFragmentActivity

时间:2014-08-18 10:51:14

标签: android

我在运行时遇到了异常。它显示java.lang.illegalStateException VideoListFragment must be attached to SherlockFragmentActivity。我不知道如何解决这些问题。

下面我发布了代码。

logcat的:

08-18 06:00:39.086: E/AndroidRuntime(3068): FATAL EXCEPTION: main
08-18 06:00:39.086: E/AndroidRuntime(3068): Process: com.loco.android, PID: 3068
08-18 06:00:39.086: E/AndroidRuntime(3068): java.lang.IllegalStateException: VideoListFragment must be attached to a SherlockFragmentActivity.
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.actionbarsherlock.app.SherlockFragment.onAttach(SherlockFragment.java:25)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.sit.loco.frgment.VideoListFragment.onAttach(VideoListFragment.java:178)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.View.measure(View.java:16497)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.View.measure(View.java:16497)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.View.measure(View.java:16497)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.View.measure(View.java:16497)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.View.measure(View.java:16497)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.os.Handler.handleCallback(Handler.java:733)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.os.Looper.loop(Looper.java:136)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at java.lang.reflect.Method.invoke(Method.java:515)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-18 06:00:39.086: E/AndroidRuntime(3068):     at dalvik.system.NativeStart.main(Native Method)

VideoListFragment.java:

  public class VideoListFragment extends SherlockFragment {

    OnVideoSelectedListener mCallback;

    boolean loadingMore = false;

    // create string variables
    String YOUTUBE_USERNAME = "";

// create object of views
    ListView list;
    ProgressBar prgLoading;
    Button btnRefresh;
    Button btnLoadMore;

    // create variable to get position, connection status, resources, and channel username
    int position;
    boolean isConnect = true;
    Resources res;
    String[] ChannelUsername;

    // create object of custom adapter
    VideoListAdapter vla;

    // create arraylist variables
    ArrayList<HashMap<String, String>> menuItems;
    ProgressDialog pDialog;

    // flag for current page
    int current_page = 1;
    int previous_page;


    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_THUMBNAIL = "thumbnail";
    static final String KEY_DURATION = "duration";

    // create interface listener
    public interface OnVideoSelectedListener{
        public void onVideoSelected(String ID);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_list, container, false);

        // connect view objects and view id on xml
        list = (ListView) v.findViewById(R.id.list);
        btnRefresh = (Button) v.findViewById(R.id.btnRefresh);


        menuItems = new ArrayList<HashMap<String, String>>();

        // get value that passed from previous page
        Bundle bundle = this.getArguments();
        position = bundle.getInt("position", 0);


        // get channel username and put selected channel to youtube api
        res = getActivity().getResources();
        ChannelUsername = res.getStringArray(R.array.channel_username);
        YOUTUBE_USERNAME = ChannelUsername[position];



        // create LoadMore button
        btnLoadMore = new Button(getActivity());
        btnLoadMore.setBackgroundResource(R.drawable.btn_default_holo_light);
        btnLoadMore.setText(getString(R.string.load_more));

        // adding load more button to lisview at bottom
        list.addFooterView(btnLoadMore);

        new loadFirstListView().execute();

        // listener to handle load more buttton when clicked
        btnLoadMore.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Starting a new async task
                isConnect = true;
    //            new loadMoreListView().execute();
            }
        });


        // listener to handle list when clicked
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                HashMap<String, String> item = new HashMap<String, String>();
                item = menuItems.get(position);

                mCallback.onVideoSelected(item.get("id"));

                list.setItemChecked(position, true);
            }
        });

        // listener to handle refresh button when clicked
        btnRefresh.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                isConnect = true;
                new loadFirstListView().execute();
            }
        });


        return v;
    }



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);  ----> Error occurred here

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnVideoSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }



}

SherlockFrgament.java:

package com.actionbarsherlock.app;

import android.app.Activity;
import android.support.v4.app.Fragment;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import static android.support.v4.app.Watson.OnCreateOptionsMenuListener;
import static android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import static android.support.v4.app.Watson.OnPrepareOptionsMenuListener;

public class SherlockFragment extends Fragment implements OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener, OnOptionsItemSelectedListener {
    private SherlockFragmentActivity mActivity;

    public SherlockFragmentActivity getSherlockActivity() {
        return mActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        if (!(activity instanceof SherlockFragmentActivity)) {
            throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity."); -->Error occurred here
        }
        mActivity = (SherlockFragmentActivity)activity;

        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }

    @Override
    public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
        onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        //Nothing to see here.
    }

    @Override
    public final void onPrepareOptionsMenu(android.view.Menu menu) {
        onPrepareOptionsMenu(new MenuWrapper(menu));
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        //Nothing to see here.
    }

    @Override
    public final boolean onOptionsItemSelected(android.view.MenuItem item) {
        return onOptionsItemSelected(new MenuItemWrapper(item));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Nothing to see here.
        return false;
    }
}

我不知道如何解决这个illegalStateException Error.Anybody可以帮我解决这个问题。谢谢。

0 个答案:

没有答案