我期待从viewPager调用片段函数

时间:2016-12-28 13:09:56

标签: android android-fragments android-viewpager

我试图在GameScreen(主要活动)中调用函数frag.updateButtons()等,每秒十次,游戏循环这些并相应地更新变量。

代码就像现在一样。 frag。***函数在设置为片段时起作用,但现在我想将它用作viewPager,它会一直出现以下错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)'

我对编程和Android代码相当新,显然不完全理解片段和viewPager等,所以非常感谢这里的任何帮助。

我没有包含整个代码,只是我认为与问题相关的代码。如果您需要更多代码或信息,请询问我,我会告诉您所需的信息。

谢谢

public class GameScreen extends FragmentActivity implements GameScreenFragment.FragInterface{

    ViewPager mViewPager;
    GameScreenFragment frag;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.game_screen);

            mViewPager = (ViewPager) findViewById(R.id.bottomFragment);
            /** set the adapter for ViewPager */
            mViewPager.setAdapter(new SamplePagerAdapter(getSupportFragmentManager()));

           startRepeatingTask();
           //the code here calls the mainLoop() function 10 times a second
        }
    }

    //for the fragment functions
    public void displayCompany(String companyName){}
    public void displayCompanyIncome(){}
    public void checkTeamImage(){}
    public void getCostOfEmployees(){}
    public void updateButtons(){}

    public void mainLoop(){
        //display company name and company income in fragment
        frag = (GameScreenFragment) getSupportFragmentManager().findFragmentById(R.id.bottomFragment);
        frag.updateButtons();
        frag.displayCompanyIncome();
        frag.getCostOfEmployees();
        frag.displayCompany(Global.companyName[Global.companyNumber]);
        //updates the picture for that company
        frag.checkTeamImage();
    }
    public class SamplePagerAdapter extends FragmentPagerAdapter {

        public SamplePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            /** Show a Fragment based on the position of the current screen */
            Global.companyNumber = position;
            return new GameScreenFragment();

        }   

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }
    }

}

然后在.xml中ID为bottomFragment

的片段中
public class GameScreenFragment extends Fragment {

    FragInterface fragStuff;

    public interface FragInterface {
        void displayCompany(String companyName);
        void displayCompanyIncome();
        void checkTeamImage();
        void getCostOfEmployees();
        void displayCompanyName();
        void updateButtons();

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.game_screen_fragment, container, false);

        //other code here
        return view;
    }
}

0 个答案:

没有答案