在我的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>
为什么空?为什么图表不被实例化?
答案 0 :(得分:1)
基本上,您在父活动中找到视图而不是片段视图。
应该是
onCreateView() {
View view = inflator.inflate(....);
chart = (LineChart) view.findViewById( R.id.chart_id );
return view;
}