【问题标题】:Unable to "findViewById" graphview无法“findViewById”graphview
【发布时间】: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


    【解决方案1】:

    您在扩展布局之前正在执行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()。如果该 Fragment 未附加到 Activity,这将导致其他问题。在onViewCreated() 中使用view 或在其他地方使用getView()

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多