谷歌滑动菜单与动作栏sherlock单独的片段菜单项

时间:2013-08-05 23:28:56

标签: android fragment slidingmenu

尝试使用操作栏sherlock实现Google滑动菜单时出现此错误。我希望每个片段都有单独的菜单项。它在onCreateOptions菜单和onOptionsItemSelected上给出了这个错误,但仅在Fragment中;它在FragmentActivity中运行良好。

The method onCreateOptionsMenu(Menu, MenuInflater) of type Fragment_1 must override or implement a supertype method

这是片段的代码

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_1 extends Fragment{


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

// ...

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.find_movie, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // ...
}


}

这是片段活动的代码。不确定它是否与问题相关。

public class MainActivity extends SherlockFragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mFragmentTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();
    mFragmentTitles = getResources().getStringArray(R.array.fragments);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    mDrawerList = (ListView)findViewById(R.id.drawer_list);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mFragmentTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, 
            mDrawerLayout, 
            R.drawable.ic_drawer, 
            R.string.drawer_open, 
            R.string.drawer_close){
        public void onDrawerClosed(View v){
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu();
        }
        public void onDrawerOpened(View v){
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    if (savedInstanceState == null){
        selectItem(0);
    }

}

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    //menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case android.R.id.home:
        if (mDrawerLayout.isDrawerOpen(mDrawerList)){
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
        return true;
    case R.id.item1:
        Intent i = new Intent(MainActivity.this, FindMovie.class);
        startActivity(i);
        return true;
     case R.id.item2:

            Intent intent1=new Intent(getApplicationContext(), AddMovie.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent1);
            return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

private class DrawerItemClickListener implements ListView.OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id){
        selectItem(position);
    }
}

private void selectItem(int position){
    Fragment newFragment = new Fragment_1();
    FragmentManager fm = getSupportFragmentManager();
    switch(position){
    case 0:
        newFragment = new Fragment_1();
        break;
    case 1:
        newFragment = new Fragment_2();
        break;
    case 2:
        newFragment = new Fragment_3();
        break;
    case 3:
        newFragment = new Fragment_4();
        break;
    case 4:
        newFragment = new Fragment_5();
        break;
    }
    fm.beginTransaction()
    .replace(R.id.content_frame, newFragment)
    .commit();

    mDrawerList.setItemChecked(position, true);
    setTitle(mFragmentTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title){
    mTitle = title;
    getSupportActionBar().setTitle(title);
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

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

}

已经看过这个主题: 'Must Override a Superclass Method' Errors after importing a project into Eclipse

我的编译器版本是1.6

我的java版本1.6.0_45

1 个答案:

答案 0 :(得分:0)

忘记扩展SherlockFragment而不是Fragment

公共类Fragment_1扩展Fragment应该是 公共类Fragment_1扩展了SherlockFragment {