如何在Android中使用Achartengine库在片段内使用图表?

时间:2013-01-13 14:35:26

标签: java android android-layout charts achartengine

我有一个活动3ChartPerTabActivity,其中我有3个选项卡片段与Viepager.Each片段视图有不同的颜色。到目前为止。

当我尝试将PieChart添加到第一个标签片段布局时出现问题... 我想为每个标签创建一个图表。例如,第一个标签是饼图e.t.c。

我决定使用Achartengine库。我试过但我得到"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);

/////////////////////////////////////////////// /////////////////////////////////////

// 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);
    }
}

以下是截图:enter image description here

2 个答案:

答案 0 :(得分:5)

getView完成后,

onCreateView才能生效: http://developer.android.com/reference/android/app/Fragment.html#getView()

使用View中充气的onCreateView作为起点,在onCreateView而不是getView时“找到”您的其他观看次数( )。

例如:

View view = (LinearLayout) inflater.inflate(R.layout.tab_frag1_layout,
                container, false);
LinearLayout chartContainer = (LinearLayout) view.findViewById(
        R.id.chart_container);
...
return view;

(假设“tab_frag1_layout”里面有“chart_container”布局。)

答案 1 :(得分:3)

在我的情况下,我使用SherlockFragment并遇到了同样的问题。

LinearLayout替换为ViewGroup后,会显示图表。

ViewGroup chartContainer = (ViewGroup)view.findViewById(R.id.chart_container);

我希望它对你和其他有同样问题的人有用。