先展示布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/chart_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
android:gravity="center"
android:textSize="30sp"/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在使用LineChart的时候如果出现这类的报错:
先确定 LineChart 的 findViewById 是否正确:
mChart = (LineChart) view.findViewById(R.id.chart);
这个要和你的 LineChart 布局文件的 id 名匹配。
我是用 ViewPager 调用 LineChart,所以下面的LineChart代码都是在 fragment v4 的包里写的。
上面确认无误之后,还是没有解决上面的报错就看一下这两段代码:
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chart_item,container, false);
initView(view);
return view;
}
在导入 LineChart 布局之后,一定要写 container 。
然后是第二行代码:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
...
}
要使用 onViewCreated 这个方法。
以上就是解决LineChart在设置 setExtraOffsets 方法和 animateX 时产生空指针的办法。
LineChart 设置 animate 这个的时候不是只有X轴,所以解决方法都是能用的。