我有一個活動3ChartPerTabActivity,其中我有3個選項卡片段與Viepager.Each片段視圖只是不同的顏色。到目前爲止工作。如何使用Achartengine庫在Android中使用圖表內的片段?
當我嘗試向第一個選項卡片段佈局添加PieChart時,會出現問題... 我想爲每個選項卡製作圖表。例如,第一個選項卡爲餅圖e.t.c.
我決定使用Achartengine
library.I嘗試但我得到"NullPointerException"
。
這裏是tab_frag1_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/chart_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
這裏是Tab1Fragment:
import java.text.DecimalFormat;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.SeriesSelection;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Tab1Fragment extends Fragment {
private GraphicalView mChartView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
// Pie Chart Slice Names
final String[] status = new String[] { "Normal", "High", "Over" };
// Pie Chart Slice Values
double[] distribution = { 6, 3, 1 };
// Color of each Pie Chart Slices
int[] colors = { Color.GREEN, Color.YELLOW, Color.RED };
// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries(" General ");
for (int i = 0; i < distribution.length; i++) {
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(status[i], distribution[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for (int i = 0; i < distribution.length; i++) {
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
// Adding the renderer of a slice to the renderer of the pie chart
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setChartTitle("General");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
//////////////////// ///////這裏有空指針異常/////////////////////
// Getting a reference to view group linear layout chart_container
LinearLayout chartContainer = (LinearLayout) getView().findViewById(
R.id.chart_container);
///////// ////////////////////////////////////////////////// /////////////////////////0
// Getting PieChartView to add to the custom layout
mChartView = ChartFactory.getPieChartView(getActivity(),
distributionSeries, defaultRenderer);
// Adding the pie chart to the custom layout
chartContainer.addView(mChartView);
return (LinearLayout) inflater.inflate(R.layout.tab_frag1_layout,
container, false);
}
}
這裏有一個截圖:
是的謝謝!現在它不會崩潰,但我沒有看到任何圖! – oikonomopo
我還沒有和achartengine一起工作過,所以我不確定你爲什麼看不到圖表,對不起。你可以嘗試一些更簡單的視圖來確保它的工作。例如,只需將一個TextView或類似的東西放在片段中,並抓住/使用它來設置「Hello World」,以確保管道是正確的,然後轉到圖表的東西。 –
好的謝謝你的幫助! – oikonomopo