添加导航标签到Android应用程序?

时间:2014-01-29 15:47:51

标签: android tabs navigation

我正在学习Android应用程序开发并获得了this文章。我的应用程序中的所有文件都按照之前的教程顺序进行。现在我正在尝试在this教程后面向应用添加导航标签。到目前为止,我已经创建了TabListener.java并将以下代码粘贴到其中:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;


public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;


/** Constructor used each time a new tab is created.
  * @param activity  The host Activity, used to instantiate the fragment
  * @param tag  The identifier tag for the fragment
  * @param clz  The fragment's Class, used to instantiate the fragment
  */
public TabListener(Activity activity, String tag, Class<T> clz) {
    mActivity = activity;
    mTag = tag;
    mClass = clz;
}

/* The following are each of the ActionBar.TabListener callbacks */

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    }
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(mFragment);
    }
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // User selected the already selected tab. Usually do nothing.
}

}

现在我应该在教程中提到粘贴以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Notice that setContentView() is not used, because we use the root
    // android.R.id.content as the container for each fragment

// setup action bar for tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);

Tab tab = actionBar.newTab()
                   .setText(R.string.artist)
                   .setTabListener(new TabListener<ArtistFragment>(
                           this, "artist", ArtistFragment.class));
actionBar.addTab(tab);

tab = actionBar.newTab()
               .setText(R.string.album)
               .setTabListener(new TabListener<AlbumFragment>(
                       this, "album", AlbumFragment.class));
actionBar.addTab(tab);
}

我尝试在MainActivity.java和TabListener.java中粘贴它但是它会抛出错误(即使在ctrl + shift + o之后) 当我将它粘贴到MainActivity.java中时,我的onCreate函数如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab()
                       .setText(R.string.artist)
                       .setTabListener(new TabListener<ArtistFragment>(
                               this, "artist", ArtistFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
                   .setText(R.string.album)
                   .setTabListener(new TabListener<AlbumFragment>(
                           this, "album", AlbumFragment.class));
    actionBar.addTab(tab);

}

错误是:

1.Multiple markers at this line
- The method getSupportActionBar() is undefined for the type 
 MainActivity
- ActionBar cannot be resolved to a type
2.artist cannot be resolved or is not a field
3.Multiple markers at this line
- TabListener cannot be resolved to a type
- ArtistFragment cannot be resolved to a 
 type
4.ArtistFragment cannot be resolved to a type
5.album cannot be resolved or is not a field
6.Multiple markers at this line
- AlbumFragment cannot be resolved to a 
 type
- TabListener cannot be resolved to a type
7.AlbumFragment cannot be resolved to a type

0 个答案:

没有答案