我正在使用 AChartEngine 制作图表,到目前为止似乎运行良好。唯一能让这个完美的事情就是能够把这个图表放在一个弹出窗口上。不幸的是,我不知道该怎么做。每当我尝试在弹出窗口中包含要调用的图表函数时,我都会得到 NullPointerException。这是我的弹出窗口的代码(每当我单击按钮时都会调用它)
void popupWindow(){
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.activity_report_popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(plotsButton, 50, -30);
}
这是相关的弹出布局xml代码:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popup"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a simple popup" />
<Button
android:id="@+id/dismiss"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/chart_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title" >
</LinearLayout>
编辑:决定还添加图表代码:
private void openChart(){
Date[] dt = new Date[dates.size()];
float[] visits = new float[dates.size()];
for(int i=0;i<dates.size();i++){
try {
dt[i]= new SimpleDateFormat("yyyy-MM-dd").parse(dates.get(i));
visits[i] = Float.parseFloat(distance.get(i));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TimeSeries visitsSeries = new TimeSeries("Visits");
for(int i=0;i<dt.length;i++){
visitsSeries.add(dt[i], visits[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(visitsSeries);
XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
visitsRenderer.setColor(Color.BLACK);
visitsRenderer.setPointStyle(PointStyle.CIRCLE);
visitsRenderer.setFillPoints(true);
visitsRenderer.setLineWidth(2);
visitsRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.addSeriesRenderer(visitsRenderer);
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
mChart = (GraphicalView) ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer,"dd-MMM-yyyy");
multiRenderer.setClickEnabled(true);
multiRenderer.setSelectableBuffer(10);
chartContainer.addView(mChart);
}
编辑 2:添加了错误日志:
05-16 10:45:39.013: E/AndroidRuntime(6311): FATAL EXCEPTION: main
05-16 10:45:39.013: E/AndroidRuntime(6311): Process: com.example.jfitnessfunctiontester, PID: 6311
05-16 10:45:39.013: E/AndroidRuntime(6311): java.lang.NullPointerException
05-16 10:45:39.013: E/AndroidRuntime(6311): at activities.ReportListViewActivity.openChart(ReportListViewActivity.java:256)
05-16 10:45:39.013: E/AndroidRuntime(6311): at activities.ReportListViewActivity.popupWindow(ReportListViewActivity.java:115)
05-16 10:45:39.013: E/AndroidRuntime(6311): at activities.ReportListViewActivity$2.onClick(ReportListViewActivity.java:137)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.view.View.performClick(View.java:4438)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.view.View$PerformClick.run(View.java:18422)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.os.Handler.handleCallback(Handler.java:733)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.os.Handler.dispatchMessage(Handler.java:95)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.os.Looper.loop(Looper.java:136)
05-16 10:45:39.013: E/AndroidRuntime(6311): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 10:45:39.013: E/AndroidRuntime(6311): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 10:45:39.013: E/AndroidRuntime(6311): at java.lang.reflect.Method.invoke(Method.java:515)
05-16 10:45:39.013: E/AndroidRuntime(6311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 10:45:39.013: E/AndroidRuntime(6311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 10:45:39.013: E/AndroidRuntime(6311): at dalvik.system.NativeStart.main(Native Method)