How to use chart inside fragment in Android using Achartengine library?

天涯浪子 提交于 2019-11-29 23:32:51

问题


I have an activity 3ChartPerTabActivity where i have 3 tabs fragment with Viepager.Each fragment view has just different color.So far is working.

My problem occurs when i try to add a PieChart to first tab fragment layout... I want to make a chart per tab.For example first tab a Pie chart e.t.c.

I decided to use Achartengine library.I tried but i get "NullPointerException".

Here is the 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>

Here is the 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);

///////////////////////////Here i have null pointer exception/////////////////////

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

Here is an screenshot :


回答1:


getView doesn't work until AFTER onCreateView completes: http://developer.android.com/reference/android/app/Fragment.html#getView()

Use the View that you inflate in onCreateView as a starting point to "find" your other views while you're still in onCreateView, instead of getView().

For example:

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

(Assuming "tab_frag1_layout" has the "chart_container" layout inside it.)




回答2:


In my case, I use the SherlockFragment and had the same problem.

After replacing the LinearLayout to ViewGroup, it shows the graph.

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

I hope it works for you and others who have the same problem.



来源:https://stackoverflow.com/questions/14304489/how-to-use-chart-inside-fragment-in-android-using-achartengine-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!