再次问,因为上一个主题没有回答...... 我试图使用FragmentPagerAdapter,但在尝试运行时 - 出现错误:
02-22 22:48:53.328:W / dalvikvm(830):无法解决超类 Linfo / androidhive / tabsswipe / SlideActivity $ FragmentPagerAdapter; (94) 02-22 22:48:53.328:W / dalvikvm(830):班级的联系 'Linfo / androidhive / tabsswipe / SlideActivity $ FragmentPagerAdapter;' 失败02-22 22:48:53.328:E / dalvikvm(830):找不到上课 'info.androidhive.tabsswipe.SlideActivity $ FragmentPagerAdapter', 从方法中引用 info.androidhive.tabsswipe.SlideActivity.onCreate
我的代码:
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.SlideFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class SlideActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new FragmentPagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_screen_slide, menu);
menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);
MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
(mPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
? R.string.action_finish
: R.string.action_next);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
return true;
case R.id.action_next:
// will do nothing.
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
}
return super.onOptionsItemSelected(item);
}
public static class FragmentPagerAdapter extends FragmentStatePagerAdapter {
public FragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return SlideFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
已经尝试构建路径,清理并导入android.support.v13.app.FragmentPagerAdapter;
感谢。
答案 0 :(得分:1)
您输入错误:
import android.app.Fragment;
import android.app.FragmentManager;
应该是
import android.support.v4.app.FragmentManager
import android.support.v4.app.Fragment
和
mPagerAdapter = new FragmentPagerAdapter(getFragmentManager());
应该是
mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager());
您可以查看FragmentActivity
here中的文档。