0

我对如何做到这一点缺乏一些了解,而且我见过的大多数例子似乎不适用于我的情况,或者完全让我感到困惑。

我的问题: 我有4个片段,2个用于数据提交,2个用于数据审查和数据计算。我的代码能够从最后两个片段中提取的唯一方法似乎是来自 main FragmentActivity。问题是,当 Activity 通过 onCreate 方法进入时,并不是我的所有片段都被加载,当我尝试声明一个时,onClickListener我得到一个NullPointerException,因为该页面尚未加载。

现在我确实setOffscreenPageLimit设置为 3,所以这根本不应该是一个问题,但我似乎无法弄清楚如何让页面在活动开始时加载。

代码:

导致应用程序强制关闭的行是声明/执行btnEnterChemicals

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_chemicals);

    //declarations
    ...
    some declarations
    ...

    //Set up ViewPager and allow app to hold prior pages when off screen
    ChemicalsAdapter = new ResultsPagerAdapter(getSupportFragmentManager());
    ChemicalsPager = (ViewPager) findViewById(R.id.chemicals_pager);        
    ChemicalsPager.setAdapter(ChemicalsAdapter);
    ChemicalsPager.setOffscreenPageLimit(3);

    //When SwipeReviewResults is displayed, fill EditText's with data from other fragments
    ChemicalsPager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {

            if (position == 2) {
                //Pull values from Chemicals 1
                ...
                some assignments
                ...

                //Pull values from Chemicals 2
                ...
                some assignments
                ...

                //Declare rvw text boxes for Meter Review 1
                ...
                some assignments
                ...

                //Declare rvw text boxes for Meter Review 2
                ...
                some assignments
                ...

                //If data isn't null, perform calculations on client entries
                if (data != null)
                {                       
                    //Do calculations before displaying values in review pages
                    ...
                    some calculations
                    ...


                    //Display results
                    ...
                    display results
                    ...
                }

                //Copy entered data to Chemicals Review 1
                ...
                display results
                ...

                //Copy entered data to Chemicals Review 2
                ...
                display results
                ...

            }
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) { }
        @Override
        public void onPageScrollStateChanged(int arg0) { }
    });

    //Insert data into database
    class insertNew implements Runnable
    {
        ...
        data submission
        ...
    }

    //Declare and set OnClickListener for Entering Results
    final Button btnEnterChemicals = (Button) ChemicalsPager.findViewById(R.id.btnEnterChemicals);
    btnEnterChemicals.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //set and encode url for data submission
            ...
            prepare data
            ...

            //Declare and start thread
            Thread thrInsertNew = new Thread(new insertNew(url));
            thrInsertNew.start();

        }
    });

提前感谢您的所有帮助!

4

1 回答 1

0

您的 onClickListener 不应该在包含所述按钮的片段的 onCreate 方法上实现吗?

在到目前为止我看到的示例中,您为要实现的每个选项卡创建一个片段(Java 类和 XML 文件),然后告诉 PagerAdapter 哪个对应于哪个位置。正如您所说,在页面加载之前,btnEnterChemicals 按钮不存在,因此添加它的正确方法是 onCLickListener 将在相应片段的 onCreate 方法中。我不确定这是否正确解决了您的问题。请告诉我。

PS:尝试阅读本文以获取有关如何在 Fragment 和包含它们的 Activity 之间进行通信的帮助。

于 2013-08-21T23:34:26.360 回答