-1

I need to call a method in which I parse the JSON objects and set the values I sets values in TextFields and button values, I created fragments but after creating the fragments how will I change the text field values

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        //Switch to different layout accordingly
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1: {
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
            }
            case 2: {
                rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
                break;
            }

            case 3: {
                rootView = inflater.inflate(R.layout.fragment_totals, container, false);
                break;
            }
            case 4: {
                rootView = inflater.inflate(R.layout.fragment_keyboard, container, false);
                break;
            }
            case 5: {
                rootView = inflater.inflate(R.layout.fragment_keylock, container, false);
                break;
            }

        }
        return rootView;
    }
}

I tried to call the method in onStart but onStart method is been called first before the onCreateView and hence I get null pointer exception when I am assigning the value :

@Override
  public void onStart() {
    super.onStart();
    parseJsonResponse(response_message);
  }

In the parseJsonResponse() method, I sets the value

String UIFooter = ResponseHeaderObject.getString("FOOTER");
//Set Header Display
headerTextView =(TextView) findViewById(R.id.headerTextView);
headerTextView.setText(UIHeader);

here is the error from logcat:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.mpos.kits.smartpos.PosMainActivity.parseJsonResponse(PosMainActivity.java:366)
Jerry Abraham
  • 1,039
  • 18
  • 42
  • There is literally a method called `onViewCreated` in a fragment. Had you only googled the title of your question, you would've found it. – Vucko Oct 21 '18 at 11:46
  • 1
    Possible duplicate of [Difference between onCreateView and onViewCreated in Fragment](https://stackoverflow.com/questions/25119090/difference-between-oncreateview-and-onviewcreated-in-fragment) – Vucko Oct 21 '18 at 11:48

2 Answers2

2

Fragments have a function named: onViewCreated which called after your view was created

Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
1

Yes, that method exists: onViewCreated. however, if you want to assign a text to the TextView outside the method on which it has been created, you have to declare the TextView as a member property (outside any method), to allow its use by all class methods. Otherwise, within a fragment, it will be difficult to manipulate it.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.mpos.kits.smartpos.PosMainActivity.parseJsonResponse(PosMainActivity.java:366)

You are getting this error because this headerTextView =(TextView) findViewById(R.id.headerTextView); returns null in your fragment unless it's referred to a root view like in the onCreateView method. this way (TextView) rootView.findViewById(R.id.section_label);

If you want to override onViewCreated method, do it this way:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

        //your code
    }
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
  • i tried using @Override public View onViewCreated(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { parseJsonResponse(response_message); } but this complaints method doesnot overide superclass method Can you show me an example for to call this onViewCreated() method – Jerry Abraham Oct 21 '18 at 12:26
  • I edited the answer to add the `onViewCreated` method – Gratien Asimbahwe Oct 21 '18 at 12:35
  • Ok I did that but now my method can not be called @Override public void onViewCreated(View v, Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); parseJsonResponse(response_message); } It gives me error non static field cannot be referenced from a static context android parseJsonResponse() is in my Activity class and I cannot change that to static method public void parseJsonResponse(String jsonResponse){// Parse the response and the textview and button values i change according to parsed values} – Jerry Abraham Oct 22 '18 at 06:42
  • 1
    @JerryAbraham, can you please declare your variables before the `onCreateView` method? as I stated in the answer – Gratien Asimbahwe Oct 22 '18 at 15:23
  • yes I did that , now my problem is that When I move from one tab to another parseJsonResponse() gets called , I want to update all the layout of the tabs regardless in which tab Iam , Is this possible?I have got 3 to 4 tabs with different layouts I want to update all the tabs regardless which tab is displayed .Please check this https://stackoverflow.com/questions/52927163/how-to-update-layout-all-layouts-in-different-tabs-in-tabs-in-android-tabs-wit – Jerry Abraham Oct 23 '18 at 07:15
  • Let me take a look but you didn't mention that the first problem or the question itself has been answered – Gratien Asimbahwe Oct 23 '18 at 07:26