-1

I'm trying to make a graph within a Fragment on my app and I am having an issue when I edit the graph the entire app crashes.

After some investigation I realized that graphView is unable to get "graph" from the XML document

public class GraphFragment extends Fragment {
@Override                                               //TODO: Make do something
 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

     GraphView graph =  getActivity().findViewById(R.id.graph); 
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3)
    });
    graph.addSeries(series);  //it crashes when the graph is edited
    return inflater.inflate(R.layout.graphview, container, false);
}}`

The Error code is displayed as

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.jjoe64.graphview.GraphView.addSeries(com.jjoe64.graphview.series.Series)' on a null object reference

The XML for graphview is as follows

' <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.jjoe64.graphview.GraphView
        android:id="@+id/graph"
        android:layout_width="match_parent"
        android:layout_height="200dip"
        android:layout_marginBottom="9dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="72dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.01" />
        </FrameLayout>'

The frame is such that there is a graph fragment within another fragment. Can someone please explain to me why the function findViewById() doesnt have the privilege to attain R.Id.graph

Skimmer6t
  • 15
  • 4

1 Answers1

2

You're doing findViewById() before you inflate the layout, meaning R.id.graph doesn't exist at that point. Change your code a bit:

@Override 
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragView = inflater.inflate(R.layout.graphview, container, false); //inflate up here and assign to variable

    GraphView graph = fragView.findViewById(R.id.graph); //change getActivity() to fragView
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3)
    });
    graph.addSeries(series);
    return fragView; //return fragView
}

Alternatively, move your code to onViewCreated():

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.graphview, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    GraphView graph = view.findViewById(R.id.graph); 
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3)
     });
     graph.addSeries(series); 
}

Just a note: don't use getActivity().findViewById() in a Fragment. If that Fragment isn't attached to the Activity, this will cause other issues. Use view in onViewCreated() or use getView() elsewhere.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63