ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Problem in getPage, return NullPointerException #28

Closed JSONValidator closed 9 years ago

JSONValidator commented 9 years ago

I get data on api and want to insert them into viewpager

adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), FragmentPagerItems.with(this)
                .add("tab1", fragment_tab_a.getClass())
                .add("tab2", fragment_tab_b.getClass())
                .add("tab3", fragment_tab_c.getClass())
                .create());

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(adapter);

        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        viewPagerTab.setViewPager(viewPager);

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    // answer
                    final String json = "";

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // after answer
                            TextView textView1 = (TextView) adapter.getPage(0).getView().findViewById(R.id.textView1);
                            textView1.setText("1 - ok");
                            TextView textView2 = (TextView) adapter.getPage(1).getView().findViewById(R.id.textView2);
                            textView2.setText("2 - ok");
                            TextView textView3 = (TextView) adapter.getPage(2).getView().findViewById(R.id.textView3);
                            textView3.setText("3 - ok");

                        }
                    });
                } catch (Exception e) {
                    //runOnUiThread(checkInternetConnection);
                    e.printStackTrace();
                }
            }
        };
        thread.start();

but I get the error java.lang.NullPointerException on

TextView textView1 = (TextView) adapter.getPage(0).getView().findViewById(R.id.textView1);

please tell me how to do it correctly project: https://www.dropbox.com/s/nfbo6uwxhlmop6j/SampleTab.zip?dl=0

ogaclejapan commented 9 years ago

Hi, @EfimenkoFedor

Yes, there is also a possibility that the null is returned. It is written in REAME.

.instantiateItem() from until .destoryItem() is called it will be able to get the Fragment of page.

To initialize after the request?


@Override
public void run() {
    try {
        // answer
        final String json = "";

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter = new FragmentPagerItemAdapter(
                            getSupportFragmentManager(), FragmentPagerItems.with(this)
                            .add("tab1", fragment_tab_a.getClass(), new Bundler().putString("result", "1-ok").get())
                            .add("tab2", fragment_tab_b.getClass(), new Bundler().putString("result", "2-ok").get())
                            .add("tab3", fragment_tab_c.getClass(), new Bundler().putString("result", "3-ok").get())
                            .create());

                ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
                viewPager.setAdapter(adapter);

                SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
                viewPagerTab.setViewPager(viewPager);            

            }
        });
    } catch (Exception e) {
        //runOnUiThread(checkInternetConnection);
        e.printStackTrace();
    }
}
JSONValidator commented 9 years ago

Now I have so:

public class MainActivity extends AppCompatActivity {

    ViewPagerItemAdapter adapter;

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

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    // answer
                    final String json = "";

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            adapter = new ViewPagerItemAdapter(ViewPagerItems.with(MainActivity.this)
                                    .add("tab1", R.layout.fragment_tab1)
                                    .add("tab2", R.layout.fragment_tab2)
                                    .add("tab3", R.layout.fragment_tab3)
                                    .create());

                            ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
                            viewPager.setAdapter(adapter);

                            SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
                            viewPagerTab.setViewPager(viewPager);

                            // after answer
                            TextView textView1 = (TextView) adapter.getPage(0).findViewById(R.id.textView1);
                            textView1.setText("1 - ok");
                            TextView textView2 = (TextView) adapter.getPage(1).findViewById(R.id.textView2);
                            textView2.setText("2 - ok");
                            TextView textView3 = (TextView) adapter.getPage(2).findViewById(R.id.textView3);
                            textView3.setText("3 - ok");

                        }
                    });
                } catch (Exception e) {
                    //runOnUiThread(checkInternetConnection);
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
}

but the error still exists

I'm sorry, I did not understand how to use instantiateItem

ogaclejapan commented 9 years ago

Page(Fragment/View) is generated at the same time as the drawing of view. instantiateItem is called from ViewPager when the necessary generation of Page(Fragment/View).

So, it should pass as an argument of Fragment To resolve this problem without too much change your implementation.


//...

adapter = getSupportFragmentManager(), FragmentPagerItems.with(this)
        .add("tab1", fragment_tab_a.getClass(), new Bundler().putString("result", "1-ok").get())
//...