我正在尝试使用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;
如果它有帮助,可以使用SlideFragment类......
package info.androidhive.tabsswipe;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A fragment representing a single step in a wizard. The fragment shows a dummy title indicating
* the page number, along with some dummy text.
*
* <p>This class is used by the {@link CardFlipActivity} and {@link
* ScreenSlideActivity} samples.</p>
*/
public class SlideFragment extends Fragment {
/**
* The argument key for the page number this fragment represents.
*/
public static final String ARG_PAGE = "page";
/**
* The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
*/
private int mPageNumber;
/**
* Factory method for this fragment class. Constructs a new fragment for the given page number.
*/
public static SlideFragment create(int pageNumber) {
SlideFragment fragment = new SlideFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public SlideFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_slide, container, false);
// Set the title view to show the page number.
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
感谢。
答案 0 :(得分:1)
您正在使用v13中的FragmentStatePagerAdapter而不是v4。那就是问题所在。在版本13中,使用的FragmentManager是android 3.5及更高版本的,在支持库的版本4中,FragmentManager是supportFragmentManager。
答案 1 :(得分:0)
如果切换到v4,只需在FragmentPagerAdapter
类上添加此构造函数即可:
/** Constructor of the class */
public FragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}