片段getView在挂起后给出null

时间:2013-01-17 18:59:54

标签: android android-fragments

为了关注这个问题,我结束了以下测试应用程序,其中包含一个活动和两个连接到FragmentStatePagerAdapter / ViewPager的片段。我每秒都有Runnable和Handler打印getView()(在生产版本中它用于在片段的TextView中显示时间)。

当我启动应用程序时,getView()显示可能很少的空(我认为由于片段生命周期)然后我得到一些非空值,这是好的...

但如果我暂停并唤醒我的设备,LogCat会一直显示空值。

public class MainActivity extends SherlockFragmentActivity {

    private SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    private Fragment myFragment1 = new MyFragment1();
    private Fragment myFragment2 = new MyFragment2();

    private Handler handler = new Handler();
    private Runnable timer;

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

        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(sectionsPagerAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();

        timer = new Runnable() {
            public void run() {
                System.out.println(myFragment1.getView()); // HERE getView gives null
                handler.postDelayed(this, 1000); // refreshing every 1 sec
            }
        };

        handler.removeCallbacks(timer);
        handler.postDelayed(timer, 0);
    }

    @Override
    protected void onPause() {
        super.onPause();

        handler.removeCallbacks(timer);
    }

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;

            if (position == 0) {
                fragment = myFragment1;
            } else {
                fragment = myFragment2;
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "Fragment 1";
            case 1:
                return "Fragment 2";
            }
            return null;
        }

    }
}

我的片段

public class MyFragment1 extends SherlockFragment {

    private TextView textView2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_layout, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ((TextView) getView().findViewById(R.id.textView1)).setText("Fragment 1");

        textView2 = (TextView) this.getView().findViewById(R.id.textView2);
    }

    public void updateTextView2(String text) {
        textView2.setText(text);
    }

1 个答案:

答案 0 :(得分:0)

好的,我只需添加以下内容即可解决我的问题:

if (myFragment1.getView() == null) {
    viewPager.setAdapter(sectionsPagerAdapter);
}

我对这个解决方案不满意但它有效......