0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
boehmP
  • 91
  • 1
  • 1
  • 11

1 Answers1

2

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);
    ...
}
Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • Thank you this helped :) However I do not understand why the question was closed as a duplicate of "What is a NullPointerException, and how do I fix it?" I know what a NullPointerException is and why they occur -.- ... But in this particular case with findviewbyId in a Fragment i was not sure because I was never using Fragments before – boehmP Aug 29 '16 at 10:18
  • @boehmP I know, a lot of people post trivial problems related to NPE and hence, its a general consensus to forward people to that post. However, I have seen that MANY good questions about NPE get the same treatment too. I might raise the issue on meta sometime. Moreover, I agree the post isn't really helpful even for novice Android Studio users. – Shaishav Aug 29 '16 at 10:22