对于FragmentPagerAdapter类型,未定义onOptionsItemSelected(MenuItem)方法

时间:2014-12-03 22:58:05

标签: java android

该应用程序工作得非常好,突然间它给了我这个错误,说明onOptionsItemSelected(MenuItem)方法未定义。我无法理解他们究竟是什么意思,我应该参考什么解决方案。

这是代码:

package com.noura.luba;

import java.util.List;
import java.util.Stack;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public  class Degree_Programs extends FragmentActivity implements TabListener{

     AppSectionsPagerAdapter mAppSectionsPagerAdapter;


        ViewPager mViewPager;
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.degree_programs);

         mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        final ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

         mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mAppSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {

                    actionBar.setSelectedNavigationItem(position);
                }
            });

            for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {

                if (i==0){
                    actionBar.addTab(
                            actionBar.newTab()
                                    .setText("Study Cycle")
                                    .setTabListener(this));
                }

                if (i==1){
                    actionBar.addTab(
                            actionBar.newTab()
                                    .setText("LMD")
                                    .setTabListener(this));
                }

                if (i==2){
                    actionBar.addTab(
                            actionBar.newTab()
                                    .setText("Business Administration")
                                    .setTabListener(this));
                }

                if (i==3){
                    actionBar.addTab(
                            actionBar.newTab()
                                    .setText("Economics")
                                    .setTabListener(this));
                }

            }
    }


    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

        mViewPager.setCurrentItem(tab.getPosition());
    }


    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new LaunchpadSectionFragment();

                default:

                    Fragment fragment = new DummySectionFragment();
                    Bundle args = new Bundle();
                    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                    fragment.setArguments(args);
                    return fragment;
            }
        }


        public int getCount() {
            return 4;
        }


        public String getPageTitle(int position) {
            return "";

    }

    public class LaunchpadSectionFragment extends Fragment {

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

            rootView.findViewById(R.id.demo_collection_button)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getActivity(), CollectionDemoActivity.class);
                            startActivity(intent);
                        }
                    });

            rootView.findViewById(R.id.demo_external_activity)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
                            externalActivityIntent.setType("image/*");
                            externalActivityIntent.addFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                            startActivity(externalActivityIntent);
                        }
                    });

            return rootView;
        }
    }

    public class DummySectionFragment extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
            Bundle args = getArguments();
            TextView dst = (TextView) rootView.findViewById(android.R.id.text1);
                    dst.setText(getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }




    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {
        case R.id.information:
            final Dialog dialog =new Dialog(Degree_Programs.this);
            dialog.setContentView(R.layout.information);
            return true;

        case R.id.logOut:

            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            return true;

        case R.id.email:

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("*/*");
            i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
            i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");

            startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public Intent createEmailOnlyChooserIntent(Intent source,
            CharSequence chooserTitle) {
        Stack<Intent> intents = new Stack<Intent>();
        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                "***@gmail.com", null));
        List<ResolveInfo> activities = getPackageManager()
                .queryIntentActivities(i, 0);

        for (ResolveInfo ri : activities) {
            Intent target = new Intent(source);
            target.setPackage(ri.activityInfo.packageName);
            intents.add(target);
        }

        if (!intents.isEmpty()) {
            Intent chooserIntent = Intent.createChooser(intents.remove(0),
                    chooserTitle);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    intents.toArray(new Parcelable[intents.size()]));

            return chooserIntent;
        } else {
            return Intent.createChooser(source, chooserTitle);
        }

    }
}

    @Override
    public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }}

1 个答案:

答案 0 :(得分:0)

@Override方法应该有onOptionsItemSelected(MenuItem)注释:

@Override
public boolean onOptionsItemSelected(MenuItem item) {