问题描述
我真的试图用FragmentPagerAdapter而是试图运行它 - 当有一个错误:
I`m trying to use FragmentPagerAdapter but when trying to run it- there is an error:
22 02-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
我的code:
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;
already tried to build path, clean and import android.support.v13.app.FragmentPagerAdapter;
有是SlideFragment类,如果它可以帮助...
There is the SlideFragment class if it could help...
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;
}
}
感谢。
推荐答案
您正在使用从V13代替V4的FragmentStatePagerAdapter。那就是问题所在。在13版所使用的FragmentManager是在Android 3.5及以上的人,在支持库的FragmentManager是supportFragmentManager第4版。
You are using the FragmentStatePagerAdapter from v13 instead of v4. Thats the problem. In version 13 the FragmentManager that is used is the one on android 3.5 and higher, In version 4 of the support library the FragmentManager is the supportFragmentManager.
这篇关于FragmentPagerAdapter - 无法解决超的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!