0

在我的应用程序中,我正在创建使用 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);

}

}
4

1 回答 1

0

将片段管理器从Activity适配器构造函数提供给适配器:

如果您使用支持库:

MyPageAdapter viewpageradapter = new MyPageAdapter(getSupportFragmentManager());
于 2013-09-28T03:14:23.160 回答