在我的应用中,我正在创建使用Fragments和pageAdapter的标签。在我的TabsViewPagerFragmentActivity类中,我收到错误:
fm cannot be resolved to a variable TabsViewPagerFragmentActivity.java /LoginAndRegistration/src/com/example/loginandregistration line 47 Java Problem
当我按照快速修复选项时,我选择创建一个局部变量然后它告诉我初始化fm(顺便说一下fm代表FragmentManager)但是然后它将它设置为 null 我在我的PageAdapter中定义了fm:
public class MyPageAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
所以我的问题是: 我应该遵循快速解决方案吗?如果是这样,我应该将 null 值更改为?或者我应该在PageAdapter课程中更改某些内容吗?这里也是TabsViewPagerFragmentActivity.java:
package com.example.loginandregistration;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import com.example.loginandregistration.MyPageAdapter;
public class TabsViewPagerFragmentActivity extends FragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from dashboard.xml
setContentView(R.layout.dashboard);
// Activate Navigation Mode Tabs
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in activity_main.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
MyPageAdapter viewpageradapter = new MyPageAdapter(fm);
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create second Tab
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
// Create third Tab
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
答案 0 :(得分:0)
提供从Activity
到适配器构造函数的片段管理器:
如果您使用支持库:
MyPageAdapter viewpageradapter = new MyPageAdapter(getSupportFragmentManager());