网格视图中的AChartEngine

时间:2014-02-10 07:40:00

标签: android gridview android-viewpager achartengine

我正在尝试使用AChartEngine库在网格视图中创建条形图和饼图。

在第一个活动中,我有一个标签主持人。在此选项卡主机的内容中,对于第一个选项卡,我已定义了网格视图。作为此网格视图的内容,我已经定义了标记为图表的相对布局。当我在调用ChartFactory后获得图表的视图时,我会将此视图添加到布局中。但是,当我在网格视图中执行此操作时,我会遇到异常。请帮忙。我的代码如下:

TabHost.xml          

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="5dp" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="5dp" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#222930" >

                    <!--
                    <android.support.v4.view.ViewPager
                        android:id="@+id/viewpager1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" />
                    -->

                    <GridView
                        android:id="@+id/gridview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:horizontalSpacing="15dp"
                        android:numColumns="1"
                        android:scrollbars="none"
                        android:verticalSpacing="15dp" />
                </LinearLayout>
</FrameLayout>
        </LinearLayout>
    </TabHost>

</FrameLayout>

grid_view_description.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/chart_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <RelativeLayout
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>

    <TextView
        android:id="@+id/no_graph"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

TabHostActivity.java

public class GraphDisplay extends TabActivity implements OnTabChangeListener {

    View tabView;
    TabHost tabHost;
    private Context tabContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph_display);
        tabContext = this;
        initTabs();
    }

    private void initTabs() {

        tabHost = (TabHost) findViewById(android.R.id.tabhost);

        tabHost.setup();

        tabHost.addTab(createTab("First", "First", R.id.tab1));

        tabHost.addTab(createTab("Second", "Second", R.id.tab2));

        tabHost.addTab(createTab("Third", "Third", R.id.tab3));

        tabHost.setCurrentTab(0);
         initPager("First");

        tabHost.setOnTabChangedListener(this);
    }

    private void initPager(String spec) {
        GridLayouts l = null;
        int view = 0;
        ViewStub stub = null;

         if ("Daily".equals(spec)) {
            Log.d("third", "inside dailly equals");
            l = GridLayouts.LDaily;
//          ViewPager dayPager = (ViewPager) findViewById(R.id.viewpager1);
            GridView dayPager = (GridView) findViewById(R.id.gridview1);
            new GetMeGraph(tabContext, "Day", l, dayPager).callAsync();
         } 
    }

    private TabSpec createTab(final String tag, final String title, final int id) {

        final View tab = LayoutInflater.from(getTabHost().getContext())
                .inflate(R.layout.tab_title_customize, null);
        ((TextView) tab.findViewById(R.id.tab_text)).setText(title);
        ImageView image = (ImageView) tab.findViewById(R.id.tab_icon);
        if (tag.equals("First")) {
            image.setBackgroundResource(R.drawable.day);
        } else if (tag.equals("Second")) {
            image.setBackgroundResource(R.drawable.month);
        } else if (tag.equals("Third")) {
            image.setBackgroundResource(R.drawable.hourb);
        }

        return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(id);
    }

    @Override
    public void onTabChanged(String arg0) {
        // setTabColor(tabHost);
        if ("Daily" == arg0) {
            tabHost.setCurrentTab(0);
            // ((ViewStub) findViewById(R.id.tab1stub)).inflate();
            initPager("Daily");
        }

    }

}

GridViewAdapter.java

public class GraphGirdAdapter extends BaseAdapter {

    private GraphicalView chart[];

    enum GridLayouts {
        LMonthly(new int[] { R.layout.activity_piegraph_usage_display,
                R.layout.activity_bargraph_usage_display }), LDaily(new int[] {
                R.layout.activity_piegraph_usage_display,
                R.layout.activity_bargraph_usage_display }), LHourly(
                new int[] { R.layout.activity_piegraph_usage_display });

        final int[] layouts;

        GridLayouts(int[] layouts) {
            this.layouts = layouts;
            Log.d("first", "init: " + this);
        }
    }

    private Context cxt;
    private final GridLayouts layouts;

    public GraphGirdAdapter(Context context, GridLayouts layouts,
            GraphicalView[] chartView) {
        cxt = context;
        this.layouts = layouts;
        chart = chartView;
        Log.d("third", "use: " + this.layouts);
    }

    @Override
    public int getCount() {
        return layouts.layouts.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(cxt, layouts.layouts[position], null);



        RelativeLayout layout = (RelativeLayout) view
                .findViewById(R.id.chart);
        if (chart != null) {
            layout.addView(chart[position]);
        } else {
            TextView noData = (TextView) view.findViewById(R.id.no_graph);
            noData.setText("Insufficient data to plot the graph");
        }
        Log.d("third", "inside instantiate: " + layouts.layouts[position]);

        return view;
    }

}

崩溃报告:

02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3351)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.addView(ViewGroup.java:3222)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.addView(ViewGroup.java:3179)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.addView(ViewGroup.java:3159)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at com.aws.datameter.GraphGirdAdapter.getView(GraphGirdAdapter.java:77)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.AbsListView.obtainView(AbsListView.java:2431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.GridView.makeAndAddView(GridView.java:1323)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.GridView.makeRow(GridView.java:328)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.GridView.fillDown(GridView.java:281)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.GridView.fillFromTop(GridView.java:403)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.GridView.layoutChildren(GridView.java:1215)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.AbsListView.onLayout(AbsListView.java:2282)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1644)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1502)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.View.layout(View.java:11420)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewGroup.layout(ViewGroup.java:4238)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.os.Looper.loop(Looper.java:137)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at android.app.ActivityThread.main(ActivityThread.java:4517)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at java.lang.reflect.Method.invoke(Method.java:511)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-10 13:08:14.509: E/AndroidRuntime(7185):     at dalvik.system.NativeStart.main(Native Method)

首先,我尝试在View Pager中实现图表。但是当我将滚动条引入条形图时,View Pager覆盖了OnTouch事件时变得一团糟。任何这些解决方案都会很棒。

0 个答案:

没有答案