Home Ask Login Register

Developers Planet

Your answer is one click away!

x10sion February 2016

Fragment Crashing whenbtrying to receive an object from an activity

Good day all,

I have an issue where my activity is making a network call and when the network call is completed, it makes some changes in the activity using the data from the JSON object received from the call, it then passes the object down to the fragments in the same activity. These fragments are in a TabLayout.

The issue I have is that if I put a System.out.println() it prints out the correct data. The minute I want to set say a TextView with the data I receive in the Fragment the app Crashes with Nullpointer. When I debug it with the Debug in Android studio, the TextView I'm setting is always null for some reason.

Activity Code that does the initial Network call:

protected void onCreate(Bundle savedInstanceState) {


private void handleIntent(Intent aIntent) {
if (aIntent != null) {

  String tradeType = aIntent.getStringExtra("itemType");
  String tradeId = aIntent.getStringExtra("itemId");

  presenter = new ItemPresenterImpl(this, ItemBuyNowActivity.this);   
  presenter.doListingServiceCall(tradeId); // <------- This is the where I send the Trade Id so I can do the network call.
} else {
  System.out.println("Intent is null in " + ItemBuyNowActivity.class.getSimpleName());


Interface between Activity and Presenter:

public interface ItemPresenter {

void doListingServiceCall(String itemId);  //<------- Comes to this Interface

void doToolbarBackgroundImageCall(TradeItem aTradeItem);


Class the implements the Presenter:

  public void doListingServiceCall(String aItemId) {  // <------- This is where the network call starts
    String homeURL = BobeApplication.getInstance().getWsURL() + mContext.getString(R.string.ws_url_item) + aItemId;


Hein February 2016

You can't call findViewById in fragment onCreate(). There is no UI at that time. Try calling findViewById in onCreateView(). Also, you have to use findViewById() on View inside fragment. You can't use Activity for this.

Like this :

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.overview_list_item_fragment, container, false);
  mOverViewHeading = (TextView) view.findViewById(R.id.frag_overview_heading_textview);
  return view;

Post Status

Asked in February 2016
Viewed 2,408 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life