Moudiz
Moudiz

Reputation: 7377

viewpager fragment returning null values

I am following up android viewpager tutorial , and I have got this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I know its saying that I am setting null value but my question why it's being null? I am returning values.

public class SingleViewActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        DemoCollectionPagerAdapter
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        ViewPager    mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }

    /*  if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            MyFragment myrag = new MyFragment();
            myrag.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, myrag).commit();
        }
*/
        // Since this is an object collection, use a FragmentStatePagerAdapter,
        // and NOT a FragmentPagerAdapter.

        public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
            public DemoCollectionPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new DemoObjectFragment();
                Bundle args = new Bundle();
                // Our object is just an integer :-P
                args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
                fragment.setArguments(args);
                return fragment;
            }

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

            @Override
            public CharSequence getPageTitle(int position) {
                return "OBJECT " + (position + 1);
            }
        }

// Instances of this class are fragments representing a single
// object in our collection.
        public static class DemoObjectFragment extends Fragment {
            public static final String ARG_OBJECT = "object";

            @Override
            public View onCreateView(LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState) {
                // The last two arguments ensure LayoutParams are inflated
                // properly.
                View rootView = inflater.inflate(
                        R.layout.fragment_collection_object, container, false);
                Bundle args = getArguments();
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(Integer.toString(args.getInt(ARG_OBJECT)));
                return rootView;
            }
        }
    }

Upvotes: 1

Views: 197

Answers (2)

B... James B.
B... James B.

Reputation: 136

You have to get a reference to the TextView using View.findViewById(R.id.the_id_of_the_view) before you can call TextView.setText() on it.

Note: I removed the 'android' in the findViewById() method.

Upvotes: 1

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Change

((TextView) rootView.findViewById(android.R.id.text1)).setText(...)

to

((TextView) rootView.findViewById(R.id.text1)).setText(...)

where @+id/text1 is the id of your textview.

Upvotes: 1

Related Questions