width and height must be > 0 error when using MPAndroidChart library

家住魔仙堡 提交于 2019-12-24 12:33:42

问题


I wanted to use MPAndroidChartLibrary in my application, but I am experiencing problems which I can't solve on my own. I would like to put the chart into the CardView which is stored inside ListView. CardView's xml looks like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="16dp"
        card_view:cardUseCompatPadding="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/textDateStartTitle"
                android:paddingLeft="5dp"
                android:textSize="26sp"
                android:textColor="@color/accent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:gravity="left"/>

            <com.github.mikephil.charting.charts.PieChart
                android:id="@+id/pie_chart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

When opening the fragment containing these CardViews I get an error:

java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:808) at android.graphics.Bitmap.createBitmap(Bitmap.java:787) at android.graphics.Bitmap.createBitmap(Bitmap.java:754) at com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:2197)

When I set android:layout_width and android:layout_height to some fixed values (eg. 200dp) inside com.github.mikephil.charting.charts.PieChart the chart looks ok and there is no error. How to get rid of this error while having width and height depending on the parent/content?


回答1:


Right now, your chart height does not depend on the height of its parent LinearLayout:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

Try this instead:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />

The second set of attributes will allow your PieChart to fill all the vertical height available inside the container LinearLayout (after accounting for the height of the TextView above it).




回答2:


The only way I found was to attach the saving of file to gallery in onclick of a button. It works perfectly after that. I guess Once the view is fully drawn, the system have correct height and width.



来源:https://stackoverflow.com/questions/27851881/width-and-height-must-be-0-error-when-using-mpandroidchart-library

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