0

アクションバー sherlock を使用して Google スライド メニューを実装しようとすると、このエラーが発生します。フラグメントごとに個別のメニュー項目が必要です。onCreateOptions メニューと onOptionsItemSelected でこのエラーが発生しますが、フラグメントでのみ発生します。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) {
    // ...
}


}

これは Fragment Activity のコードです。質問に関連するかどうかはわかりません。

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

4

1 に答える 1

0

Fragment の代わりに SherlockFragment を拡張するのを忘れました

public class Fragment_1 extends Fragment は public class Fragment_1 extends SherlockFragment {

于 2013-08-06T18:10:59.547 に答える