gpt4 book ai didi

java - lineChartView 在 fragment 中findViewById

转载 作者:行者123 更新时间:2023-12-02 05:01:07 25 4
gpt4 key购买 nike

当我尝试在 fragment 中添加图表时出现一些错误。我正在尝试在 fragment 中制作一个图表,该 fragment 将引用我在 XML 中为 fragment 创建的 LineChartView 元素。

我使用 hellochart 并阅读了教程。在那里解释了使用 Activity 来实现它。我如何将其实现为 fragment ?

LineChartView lineChartView;
String[] axisData = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept",
"Oct", "Nov", "Dec"};
int[] yAxisData = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

lineChartView = findViewById(R.id.chart);
List yAxisValues = new ArrayList();
List axisValues = new ArrayList();
Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));
for (int i = 0; i < axisData.length; i++) {
axisValues.add(i, new AxisValue(i).setLabel(axisData[i]));
}
for (int i = 0; i < yAxisData.length; i++) {
yAxisValues.add(new PointValue(i, yAxisData[i]));
}
List lines = new ArrayList();
lines.add(line);
LineChartData data = new LineChartData();
data.setLines(lines);
Axis axis = new Axis();
axis.setValues(axisValues);
axis.setTextSize(16);
axis.setTextColor(Color.parseColor("#03A9F4"));
data.setAxisXBottom(axis);
Axis yAxis = new Axis();
yAxis.setName("Sales in millions");
yAxis.setTextColor(Color.parseColor("#03A9F4"));
yAxis.setTextSize(16);
data.setAxisYLeft(yAxis);
lineChartView.setLineChartData(data);
Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
viewport.top = 110;
lineChartView.setMaximumViewport(viewport);
lineChartView.setCurrentViewport(viewport);
return inflater.inflate(R.layout.fragment_events, container, false);
}

findViewById 方法有一个错误,表明该方法无法解析。

最佳答案

更改 onCreateView 并告诉我它是否有效。首先声明View

private View paramView;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {

paramView = inflater.inflate(R.layout.fragment_events, container, false);
lineChartView = paramView.findViewById(R.id.chart);
// Here comes all your code
return paramView;
}

关于java - lineChartView 在 fragment 中findViewById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56385717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com