如何使用Achartengine库使用图表片段里面的Andr​​oid? [英] How to use chart inside fragment in Android using Achartengine library?

查看:154
本文介绍了如何使用Achartengine库使用图表片段里面的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我决定使用 Achartengine library.I试过,但我得到NullPointerException异常

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

下面是tab_frag1_layout.xml:

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>

下面是Tab1Fragment:

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 :

推荐答案

getView 不工作,直到 onCreateView 完成: http://developer.android.com/reference/android/app/Fragment html的#getView ()

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

使用了查看您在 onCreateView 膨胀为出发点,以发现你的其他观点,而你'重新仍然在 onCreateView ,而不是 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().

例如:

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

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

这篇关于如何使用Achartengine库使用图表片段里面的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆