Search code examples
javaandroidandroid-fragmentsnullpointerexceptionfindviewbyid

NullPointerException when using findViewById to set text from a Bundle on Textview in a Fragment class


I researched this problem on various sites and questions here on stack overflow, but I could not find a solution that solved my problem. I am struggling with this problem for some time now but just can't solve it..

I have two activities and a fragment. The first Activity (Overview) should add the Fragment in t's onCreate with different text then the default one. The second Activity (AddCity) sends this text data to Overview with an Intent and a Bundle. In Overview the Data is available and i send it to the Fragment with myfragment.setArguments(bundle), but when I try to access the Textview in onCreateView with Bundle bundle = getArguments() i get the following error:

FATAL EXCEPTION: main
     Process: com.myapp.www, PID: 19690
     java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

The exception occurs at the following line in StatusFragment:

TextView cityText = (TextView) getView().findViewById(R.id.city_name);

I already tried the approach using a own constructor for that, but as far as i know you should avoid a custom constructor other than the empty default constructor in a fragment, and it didn't work either.

My classes are:

Overview:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.myicon);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);

        if(getIntent().getExtras() != null){
            Bundle extras = getIntent().getExtras();

            StatusFragment newFragment = new StatusFragment();
            newFragment.setArguments(extras);

            mSectionsPagerAdapter.addFragment(newFragment, extras.getString("city"));
        }else{
            Log.w("Overview-Bundle", "No Bundle Data");
        }

        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

AddCity:

This class uses a method to receive a JSON string and parse it to get the data i need to send to the fragment. This works fine, so I only give the relevant code where I put together the Intent. obj is the JSON object. (let me know if you need more code)

Intent i = new Intent(AddCity.this, Overview.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("city", obj.getString("user_city"));
                    bundle.putString("country", obj.getString("user_ country"));
                    i.putExtras(bundle);
                    startActivity(i);

StatusFragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_for_overview, null);

        setUI(view);

        return view;
    }

    public void setUI(View view){
        if(getArguments() != null) {

            Bundle bundle = getArguments();

            String city = bundle.getString("city");
            String country = bundle.getString("country");

            TextView cityText = (TextView) getView().findViewById(R.id.city_name);
            TextView countryText = (TextView) getView().findViewById(R.id.country_name);

            cityText.setText(city);
            countryText.setText(country);

        }else{
            Log.w("Arguments", "no arguments");
        }
    }

I would be grateful for every answer. Let me know if I should post more code.


Solution

  • You are calling getView() before onCreateView() has returned, hence the null pointer. In your case you can simply call:

    public void setUI(View view){
        ...
        TextView cityText = (TextView) view.findViewById(R.id.city_name);
        ...
    }