-1

I am getting a nullPointerException for when I try to change the text of a textView within a fragment I have tried suggestions of putting getView() and getActivity() before findByViewId but they both result in errors. Thank you.


public class MainFragment extends Fragment {


public MainFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_main, container, false);

    //calls setdate function
    setDate();

    return rootview;
}

public void setDate(){
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    //need to use calendar class as Date is deprecated (old)


    TextView textView = (TextView) getActivity().findViewById(R.id.info_text);
    textView.setText("updated");
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="75dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Test"
            android:textSize="25dp"
            android:textStyle="bold"
            android:textColor="#252525" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view2"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="395dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/info_text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="placeholder_text"
            android:textSize="25dp"
            android:textStyle="bold"
            android:textColor="#252525" />
    </android.support.v7.widget.CardView>

</LinearLayout>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                           at au.com.alexh.dayrater.MainFragment.setDate(MainFragment.java:43)
                                                                           at au.com.alexh.dayrater.MainFragment.onCreateView(MainFragment.java:33)

1 Answers1

0

Change your setDate to:

public void setDate(View v){
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    //need to use calendar class as Date is deprecated (old)


    TextView textView = (TextView) v.findViewById(R.id.info_text);
    textView.setText("updated");
}

and call it as :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_main, container, false);

    //calls setdate function
    setDate(rootview);

    return rootview;
}
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33