无法“ findViewById” graphview

时间:2018-10-12 20:32:21

标签: java android xml android-fragments android-graphview

我正在尝试在我的应用程序的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的特权

1 个答案:

答案 0 :(得分:2)

您需要先进行findViewById()的填充,然后再扩展布局,这意味着R.id.graph当时不存在。稍微更改一下代码:

@Override 
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragView = inflater.inflate(R.layout.graphview, container, false); //inflate up here and assign to variable

    GraphView graph = fragView.findViewById(R.id.graph); //change getActivity() to fragView
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3)
    });
    graph.addSeries(series);
    return fragView; //return fragView
}

或者,将您的代码移至onViewCreated()

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.graphview, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    GraphView graph = view.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); 
}

请注意:不要在片段中使用getActivity().findViewById()。如果该片段未附加到活动,则将导致其他问题。在view中使用onViewCreated()或在其他地方使用getView()