内容简介:它必须是简单的东西.我正在展示大部分代码.错误在下一节中.这是我的错误所在.
我看了很多代码,无法解决这个问题. http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
它必须是简单的东西.
我正在展示大部分代码.错误在下一节中.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final int TAB_COUNT = 3; public static InputMethodManager inputManager; SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding // tab. mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { actionBar.addTab(actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } // To control keyboard pop-up inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { }
这是我的错误所在.
getItem的case 1,Type mismatch:无法从MainActivity.HistoryFragment转换为Fragment.它告诉我要么将方法返回类型更改为HistoryFragment,要么将newInstance()的返回类型更改为Fragment.哪里我也做不到.我见过的其他例子与我的代码几乎相同.我曾经尝试过,不经过论证.
public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: Fragment tipCalc = new TipCalcFragment(); return tipCalc; case 1: // Fragment history = new HistoryFragment(); return HistoryFragment.newInstance(i); //ERROR HERE case 2: Fragment stats = new StatsFragment(); return stats; } return null; }
我的HistoryFragment扩展了ListFragment.最后,它不会从String []值中提取,而是从数据库资源中提取.我只是想先设置一个结构,然后看看/播放布局.
public static class HistoryFragment extends ListFragment { // public HistoryFragment() { // } String[] values = new String[] { ... }; static HistoryFragment newInstance(int num) { HistoryFragment history = new HistoryFragment(); Bundle args = new Bundle(); args.putInt("num", num); history.setArguments(args); return history; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.history, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values)); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Golang学习笔记之错误处理error、panic (抛出错误),recover(捕获错误)
- c – 构建PBRT v2错误 – 错误1错误U1077:’if’:返回代码’0x1′
- !错误!在 Android 下这么用 ShowModal 是错误的!
- Google开源ClusterFuzz:使得查找错误并修复错误变得异常简单
- 脚本错误量极致优化-定位压缩且无 SourceMap 文件的脚本错误
- php – 解析错误:语法错误,意外’未设置'(T_UNSET)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。