我的项目需要使用简单的条形图。 我尝试使用aChartEngine,我在启动图表意图中遇到错误和应用程序崩溃。 最后我可以运行任何aChartEngin示例。 任何人都可以帮我吗?
private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barchart_form);
// Getting reference to the button btn_chart
Button btnChart = (Button) findViewById(R.id.add);
// Defining click event listener for the button btn_chart
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Draw the Income vs Expense Chart
openChart();
}
};
// Setting event click listener for the button btn_chart of the
// MainActivity layout
btnChart.setOnClickListener(clickListener);
}
private void openChart() {
int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
// int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };
// Creating an XYSeries for Income
// CategorySeries incomeSeries = new CategorySeries("Income");
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Income
// XYSeries expenseSeries = new XYSeries("Expense");
// Adding data to Income and Expense Series
for (int i = 0; i < x.length; i++) {
incomeSeries.add(i, income[i]);
// expenseSeries.add(i,expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
// dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
// XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
// expenseRenderer.setColor(Color.rgb(220, 80, 80));
// expenseRenderer.setFillPoints(true);
// expenseRenderer.setLineWidth(2);
// expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setZoomButtonsVisible(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to
// multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
// multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot bar chart using dataset and
// multipleRenderer
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(),
dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
我的猜测是,以下几行导致了您的问题!
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(),
dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
我的解释是,您在使用NullPointerException
时可能有getBaseContext()
,因为当您启动新意图以打开新Activity
从而破坏活动时,活动的上下文可能会被销毁你想要获得背景的地方。
如果您将getBaseContext()
替换为getApplicationContext()
,则可以解决问题!
您的解决方案将是:
Intent intent = ChartFactory.getBarChartIntent(getApplicationContext(),
dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
希望它有效!让我知道你的进展