gpt4 book ai didi

java - FragmentPagerAdapter - 无法解析父类(super class)

转载 作者:行者123 更新时间:2023-12-01 13:22:11 25 4
gpt4 key购买 nike

我正在尝试使用 FragmentPagerAdapter,但在尝试运行它时出现错误:

02-22 22:48:53.328: W/dalvikvm(830): Unable to resolve superclass of Linfo/androidhive/tabsswipe/SlideActivity$FragmentPagerAdapter; (94) 02-22 22:48:53.328: W/dalvikvm(830): Link of class 'Linfo/androidhive/tabsswipe/SlideActivity$FragmentPagerAdapter;' failed 02-22 22:48:53.328: E/dalvikvm(830): Could not find class 'info.androidhive.tabsswipe.SlideActivity$FragmentPagerAdapter', referenced from method 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;
}
}

谢谢。

最佳答案

您使用的是 v13 中的 FragmentStatePagerAdapter,而不是 v4。那就是问题所在。在版本 13 中,使用的 FragmentManager 是 android 3.5 及更高版本上的 FragmentManager。在支持库的版本 4 中,FragmentManager 是 supportFragmentManager。

关于java - FragmentPagerAdapter - 无法解析父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com