如何使用Achartengine库使用图表内部片段的Android?(How to use char

2019-07-18 04:21发布

我有一个活动3ChartPerTabActivity在那里我有Viepager.Each片段视图3个标签片段刚刚不同color.So远工作。

出现我的问题,当我尝试一个饼图添加到第一片片段布局......我要让每tab.For例如第一个标签饼图等图表

我决定用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);

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

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

下面是一个屏幕截图:

Answer 1:

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”布局)。



Answer 2:

就我而言,我用的是SherlockFragment和有同样的问题。

更换后LinearLayoutViewGroup ,它示出了曲线图。

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

我希望它为你和其他人谁也有同样问题。



文章来源: How to use chart inside fragment in Android using Achartengine library?