首页 文章

为什么我尝试创建一个返回null的图表?

提问于
浏览
0

在我的 ScreenFragment 课程中:

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

在其 onCreate()

parentActivity = getActivity();

在其 onCreateView()

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

正在返回 null . ID在Fragment的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>

为什么空?为什么图表不被实例化?

1 回答

  • 1

    基本上,您在父活动而不是片段视图中查找视图 .

    它应该是

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

相关问题