Robert Lewis
Robert Lewis

Reputation: 1907

Why is my attempt to create a chart returning null?

In my ScreenFragment class:

Activity parentActivity; 
public LineChart chart;  // changing private to public didn't help

In its onCreate():

parentActivity = getActivity();

In its onCreateView():

chart = (LineChart) parentActivity.findViewById( R.id.chart_id );

which is returning null. The ID is defined in the Fragment's XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:padding="@dimen/fab_margin"
     tools:context="io.aeroscope.aeroscope.ScreenFragment"> 
    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

Why the null? Why isn't the chart being instantiated?

Upvotes: 0

Views: 92

Answers (1)

albeee
albeee

Reputation: 1472

Basically you're finding the view in parent activity instead of fragment view.

It should be

onCreateView() {
View view = inflator.inflate(....);
chart = (LineChart) view.findViewById( R.id.chart_id );
return view;
}

Upvotes: 1

Related Questions