【发布时间】:2020-02-07 07:41:34
【问题描述】:
我想让我的 android 小部件显示饼图。
我知道我可以使用以下方法创建饼图:
PieChartView pieChartView = findViewById(R.id.pieChart);
但是我无法将findViewById() 用于小部件。
编辑:
错误提示“无法解析方法'findViewById(?)'”
这是我迄今为止尝试过的:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
PieChartView pieChartView = findViewById(R.id.pieChart); //error occurs here
List<SliceValue> pieData = new ArrayList<>();
pieData.add(new SliceValue(25, Color.GREEN).setLabel("1"));
pieData.add(new SliceValue(25, Color.RED).setLabel("2"));
pieData.add(new SliceValue(25, Color.BLUE).setLabel("3"));
PieChartData pieChartData = new PieChartData(pieData);
pieChartData.setHasLabels(true).setValueLabelTextSize(14);
pieChartView.setPieChartData(pieChartData); //have not been able to create a PieChartView object so can't do this line
pieChartData.setAxisXBottom(new Axis());
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
但我无法使用 findByView(),因此无法创建 PieChartView 对象,因此无法使用 setPieChartData() 设置饼图数据。
【问题讨论】:
-
请分享更多您的代码或您尝试过的内容,是否有错误? findViewById() 返回 null?您面临什么样的错误/困难?
-
findViewById()在onCreateViewMethod()内部工作。这就是你的问题所在。