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