Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalArgumentException: width and height must be > 0 #351

Closed
jakehockey10 opened this issue Feb 14, 2015 · 3 comments
Closed

Comments

@jakehockey10
Copy link

Hello,

I've noticed that this same exception has happened to a few before me. But the ones with reproducible solutions have not provided the same success for me, unfortunately.

Here is my fragment layout:

<!-- Master layout. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/fragment_sensor_linear_layout"
        android:gravity="center_vertical"
        android:layout_gravity="left"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:fontFamily="sans-serif-condensed"
            android:id="@+id/detail_sensor_textview"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="@color/black"
            android:textSize="24sp"
            android:text="@string/hello_world"/>

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</ScrollView>

... and the parent activity's layout:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_sensor"
    android:name="com.example.jake.sunshine.SensorFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jake.sunshine.SensorFragment"
    tools:layout="@android:layout/list_content" />

In the SensorsActivity.java class, I @Override several methods, two of which handle the creation and inflation of the fragment, differently, depending on the size of the device:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        if (findViewById(R.id.sensor_detail_container) != null) {

            // The detail container view will be present only in the large-screen layouts
            // (res/layout-sw600dp).  If this view is present, then the activity should be
            // in two-pane mode.
            mTwoPane = true;
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            if (savedInstanceState == null) {
                SensorDetailFragment fragment = new SensorDetailFragment();
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.sensor_detail_container, fragment)
                        .commit();
            }
        } else {
            mTwoPane = false;
        }

        /*SensorFragment sensorFragment = ((SensorFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_sensor));*/
    }

    @Override
    public void onItemSelected(String sensor) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle args = new Bundle();
            args.putString(SensorDetailActivity.SENSOR_KEY, sensor);

            SensorDetailFragment fragment = new SensorDetailFragment();
            fragment.setArguments(args);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.sensor_detail_container, fragment)
                    .commit();
        } else {
            Intent intent = new Intent(this, SensorDetailActivity.class)
                    .putExtra(SensorDetailActivity.SENSOR_KEY, sensor);
            startActivity(intent);
        }
    }

I'm not sure what I need to do to avoid this exception when the bitmap is being created. The rest of this issue report is my exception trace. I appreciate any and all help! Thanks!

Process: com.example.jake.sunshine, PID: 28490
    java.lang.IllegalArgumentException: width and height must be > 0
            at android.graphics.Bitmap.createBitmap(Bitmap.java:933)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:912)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
            at com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:2197)
            at android.view.View.sizeChange(View.java:16776)
            at android.view.View.setFrame(View.java:16749)
            at android.view.View.layout(View.java:16664)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.widget.ScrollView.onLayout(ScrollView.java:2284)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16668)
            at android.view.ViewGroup.layout(ViewGroup.java:5302)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2313)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2026)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1185)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6591)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
@jakehockey10
Copy link
Author

I tried thinking of a way to get the chart added once things have been fully inflated and have their dimensions set. Unfortunately, I think I'm misunderstanding something pretty crucial to getting these charts to work. I managed to create a menu item on the SensorDetailFragment for adding the chart after the view has been fully rendered. Upon clicking the menu item, the chart is added to the LinearLayout programmatically like this (in SensorDetailFragment.java):

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_add_chart) {
            initializeChartObjects();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void initializeChartObjects() {
        LinearLayout linearLayout = (LinearLayout) getActivity().findViewById(R.id.fragment_sensor_linear_layout);
        mChart = new LineChart(getActivity());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 
                ViewGroup.LayoutParams.MATCH_PARENT);
        mChart.setLayoutParams(params);
        linearLayout.addView(mChart);
//        mChart = (LineChart) getActivity().findViewById(R.id.chart);
        int valueCountForSensor = Utility.getValueCountForSensor(mSensor.getType());
        mVals = new ArrayList<ArrayList<Entry>>(valueCountForSensor);
        for (int i = 0; i < valueCountForSensor; i++) {
            mVals.add(new ArrayList<Entry>());
        }
    }

@PhilJay PhilJay closed this as completed Feb 20, 2015
@jakehockey10
Copy link
Author

Wait a minute, why was this closed?

@PhilJay
Copy link
Owner

PhilJay commented Feb 22, 2015

Because the bug was fixed, but is not releasesd yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants