【发布时间】:2018-10-12 20:32:20
【问题描述】:
我正在尝试在我的应用程序的 Fragment 中制作图表,但我在编辑图表时遇到问题,导致整个应用程序崩溃。
经过一番调查,我意识到 graphView 无法从 XML 文档中获取“图形”
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);
}}`
错误代码显示为
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.jjoe64.graphview.GraphView.addSeries(com.jjoe64.graphview.series.Series)”
graphview的XML如下
' <?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>'
该框架使得在另一个片段中存在一个图形片段。有人可以向我解释为什么函数 findViewById() 没有获得 R.Id.graph 的特权
【问题讨论】:
标签: java android xml android-fragments android-graphview