无法解析方法getSupportFragmentManager()

时间:2016-01-05 02:27:00

标签: android android-tabhost

我收到错误 -

  

"无法解析方法getSupportFragmentManager()"

在下面的代码中。

对于getSupportFragmentManager(),我们需要扩展AppCompatActivity。但它在addTab()中出错。

public class MainActivity extends TabActivity implements OnMenuItemClickListener, OnMenuItemLongClickListener {

private static final String TAG_1 = "tab1";
private static final String TAG_2 = "tab2";
private static final String TAG_3 = "tab3";
private static final String TAG_4 = "tab4";
private static final String TAG_5 = "tab5";

TabHost mTabHost;
private FragmentManager fragmentManager;
private ContextMenuDialogFragment mMenuDialogFragment;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = new Intent(this, LoginActivity.class);
    startActivity(intent);

    fragmentManager = getSupportFragmentManager();
    initMenuFragment();

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    //mTabHost.getTabWidget().setDividerDrawable(null);
    TabHost.TabSpec tab1 = mTabHost.newTabSpec(TAG_1);
    tab1.setIndicator("Tab1");
    tab1.setIndicator(createTabIndicator("Home", createTabDrawable(R.drawable.home)));
    tab1.setContent(new Intent(this, Home.class));
    mTabHost.addTab(tab1);

    TabHost.TabSpec tab2 = mTabHost.newTabSpec(TAG_2);
    tab2.setIndicator("Tab2");
    tab2.setIndicator(createTabIndicator("Schedule", createTabDrawable(R.drawable.schedule)));
    tab2.setContent(new Intent(this, Schedule.class));
    mTabHost.addTab(tab2);

    TabHost.TabSpec tab3 = mTabHost.newTabSpec(TAG_3);
    tab3.setIndicator("Tab3");
    tab3.setIndicator(createTabIndicator("My Schedule", createTabDrawable(R.drawable.star)));
    tab3.setContent(new Intent(this, MySchedule.class));
    mTabHost.addTab(tab3);

    TabHost.TabSpec tab4 = mTabHost.newTabSpec(TAG_4);
    tab4.setIndicator("Tab4");
    tab4.setIndicator(createTabIndicator("Surveys", createTabDrawable(R.drawable.surveys)));
    tab4.setContent(new Intent(this, Surveys.class));
    mTabHost.addTab(tab4);

    TabHost.TabSpec tab5 = mTabHost.newTabSpec(TAG_5);
    tab5.setIndicator("Tab5");
    tab5.setIndicator(createTabIndicator("Menu", createTabDrawable(R.drawable.menu)));
    tab5.setContent(new Intent(this, MyMenu.class));
    mTabHost.addTab(tab5);
    getTabWidget().getChildAt(4).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMenuDialogFragment.show(fragmentManager, "ContextMenuDialogFragment");
                }
            });



}

private Drawable createTabDrawable(int resId) {
    Resources res = getResources();
    StateListDrawable states = new StateListDrawable();

    final Options options = new Options();
    options.inPreferredConfig = Config.ARGB_8888;

    Bitmap icon = BitmapFactory.decodeResource(res, resId, options);

    Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
    Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);

    icon.recycle();

    states.addState(new int[]{android.R.attr.state_selected}, new BitmapDrawable(res, selected));
    states.addState(new int[]{android.R.attr.state_enabled}, new BitmapDrawable(res, unselected));

    return states;
}

private View createTabIndicator(String label, Drawable drawable) {
    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);

    TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
    txtTitle.setText(label);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
    txtTitle.setLayoutParams(params);
    ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
    imgIcon.setImageDrawable(drawable);

    return tabIndicator;
}

//context-menu

private void initMenuFragment() {
    MenuParams menuParams = new MenuParams();
    menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
    menuParams.setMenuObjects(getMenuObjects());
    menuParams.setClosableOutside(true);
    mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    mMenuDialogFragment.setItemClickListener(this);
    mMenuDialogFragment.setItemLongClickListener(this);
}

private List<MenuObject> getMenuObjects() {

    List<MenuObject> menuObjects = new ArrayList<>();

    MenuObject close = new MenuObject();
    close.setResource(R.drawable.icn_close);

    MenuObject send = new MenuObject("Send message");
    send.setResource(R.drawable.icn_1);

    MenuObject like = new MenuObject("Like profile");
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icn_2);
    like.setBitmap(b);

    MenuObject addFr = new MenuObject("Add to friends");
    BitmapDrawable bd = new BitmapDrawable(getResources(),
            BitmapFactory.decodeResource(getResources(), R.drawable.icn_3));
    addFr.setDrawable(bd);

    MenuObject addFav = new MenuObject("Add to favorites");
    addFav.setResource(R.drawable.icn_4);

    MenuObject block = new MenuObject("Block user");
    block.setResource(R.drawable.icn_5);

    menuObjects.add(close);
    menuObjects.add(send);
    menuObjects.add(like);
    menuObjects.add(addFr);
    menuObjects.add(addFav);
    menuObjects.add(block);
    return menuObjects;
}

@Override

public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.context_menu:
            if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
        mMenuDialogFragment.dismiss();
    } else{
        finish();
    }
}

@Override
public void onMenuItemClick(View clickedView, int position) {
    Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onMenuItemLongClick(View clickedView, int position) {
    Toast.makeText(this, "Long clicked on position: " + position, Toast.LENGTH_SHORT).show();
}
}

你能帮我解释为什么会出现这个错误吗?

我会扩展哪个课程?

2 个答案:

答案 0 :(得分:1)

getSupportFragmentManagerFragmentActivity的方法,而非TabActivity,因此您只需将其替换为getFragmentManager()

答案 1 :(得分:0)

您可以像这样创建片段活动:

Scope.SUBTREE

然后你用这个:

  FragmentActivity mFragmentActivity;