我正在尝试使用HelloCharts在AlertDialog中生成饼图。我已经能够在单独的应用程序中成功生成图表,但是,当我将代码移至主应用程序并放入AlertDialog时,出现以下错误:
08-21 17:59:28.218 10363-10363/ca.rvogl.tpbcui E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.rvogl.tpbcui, PID: 10363
java.lang.NullPointerException: Attempt to invoke virtual method 'void lecho.lib.hellocharts.view.PieChartView.setPieChartData(lecho.lib.hellocharts.model.PieChartData)' on a null object reference
at ca.rvogl.tpbcui.views.SeriesActivity.showSeriesDetailsDialog(SeriesActivity.java:309)
at ca.rvogl.tpbcui.views.SeriesActivity.access$300(SeriesActivity.java:43)
at ca.rvogl.tpbcui.views.SeriesActivity$4.onClick(SeriesActivity.java:252)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1067)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121)
at android.widget.AbsListView$3.run(AbsListView.java:4036)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我尝试通过stackoverflow搜索,以及google搜索对此的任何引用,但是什么也找不到。也许有人可以在这方面给我一些见识。下面是我的alertdialog代码和xml布局,以供参考。
//Show Alert Dialog With Pie Chart Displaying Stats
private void showSeriesDetailsDialog( final Series series, final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
final View view = View.inflate(this, R.layout.dialog_seriesdetails, null);
//AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(SeriesActivity.this);
//alertDialogBuilderUserInput.setView(view);
PieChartView pieChartView = findViewById(R.id.chart);
List<SliceValue> pieData = new ArrayList<>();
Integer strikes = 9;
Integer spares = 1;
Integer splits = 1;
Integer splitconversion = 1;
Integer openframes = 0;
if (strikes < 1) {
} else {
pieData.add(new SliceValue(strikes, Color.parseColor("#33B5E5")).setLabel(strikes +" Strikes" ));
}
if (spares < 1) {
} else {
pieData.add(new SliceValue(spares, Color.parseColor("#99CC00")).setLabel(spares +" Spares"));
}
if (splits < 1) {
} else { pieData.add(new SliceValue(splits, Color.parseColor("#FF4444")).setLabel(splits +" Split"));
}
if (splitconversion < 1) {
} else {
pieData.add(new SliceValue(splitconversion, Color.parseColor("#AA66CC")).setLabel(splitconversion +" Split Conversion "));
}
if (openframes < 1) {
} else {
pieData.add(new SliceValue(openframes, Color.parseColor("#ff6f60")).setLabel(openframes +" Open Frames "));
}
PieChartData pieChartData = new PieChartData(pieData);
pieChartData.setHasLabels(true).setValueLabelTextSize(8);
pieChartData.setValueLabelBackgroundEnabled(false);
pieChartData.setHasCenterCircle(true).setCenterText1("Series Stats").setCenterText1FontSize(18).setCenterText1Color(Color.parseColor("#0097A7"));;
pieChartView.setPieChartData(pieChartData);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.show();
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="24dp" >
<lecho.lib.hellocharts.view.PieChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 0 :(得分:1)
PieChartView pieChartView = (PieChartView) view.findViewById(R.id.chart);
答案 1 :(得分:1)
在下面的行中,从 dialog_seriesdetails.xml
中将View
的{{1}}膨胀
AlertDialog
final View view = View.inflate(this, R.layout.dialog_seriesdetails, null);
是该布局的一部分。如果要将其分配给某个变量,则需要编写
PieChartView
方法PieChartView pieChartView = view.findViewById(R.idchart);
返回具有所需ID的第一个View.findViewById(int)
,可以在调用它的View
的整个View
层次结构中找到它,因此它不仅限于寻找直系子女。 (但是,如果您在View
上使用它,并传递每行中存在的ListView
的ID,那么您将无法确定它将返回什么)
但是您为什么得到View
?您的代码
NullPointerException
未使用 PieChartView pieChartView = findViewById(R.idchart);
,而是使用View.findViewById()
。 Activity.findViewById()
方法的工作原理与Activity
方法相同,但是它会搜索您在View
中传递的布局(通常是您在setContentView()
中所做的第一件事)
在onCreate()
布局中没有ID为 chart 的View
,因此 pieChartView 为Activity
。