Marc Karam
Marc Karam

Reputation: 465

App Crashing when Adding a Button- Android

I have absolutely 0 experience in android app design. A friend of mine asked me to try and create a feature for their app and that's what I've been trying to do for the past week. The first couple things I need to do are add a background image (done) add some text (done) and add a button that brings up a menu (problem). Without adding in the button I can run the app in Android Studio's built-in emulator but the instant I add the button/imageButton the app crashes. At this point I'm not even trying to get the button to bring up the menu, that's for another day. All I'm trying to do is get the button to not crash the app. I've been looking all over the internet at tutorials, documentation and even here on stack overflow but I'm not able to find anything.

Right now I haven't even started working with the Java code, I'm only working with XML code (see below). I'll post the error message (the one from the android monitor feature) and I'll post an image of what my device looks like in the studio and the message that appears on the emulator. If someone could please help me I would really appreciate it.

07-17 21:38:05.700 4273-4273/com.example.android.postvu E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.android.postvu, PID: 4273
                                                                      java.lang.RuntimeException: Canvas: trying to draw too large(110250000bytes) bitmap.
                                                                          at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
                                                                          at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
                                                                          at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
                                                                          at android.view.View.getDrawableRenderNode(View.java:17314)
                                                                          at android.view.View.drawBackground(View.java:17250)
                                                                          at android.view.View.draw(View.java:17062)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16053)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1228)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16048)
                                                                          at android.view.View.draw(View.java:16837)
                                                                          at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
                                                                          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
                                                                          at android.view.View.draw(View.java:17074)
                                                                          at com.android.internal.policy.DecorView.draw(DecorView.java:751)
                                                                          at android.view.View.updateDisplayListIfDirty(View.java:16053)
                                                                          at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
                                                                          at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
                                                                          at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
                                                                          at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
                                                                          at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

This is my XML Code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.android.postvu.MainActivity">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/background"
    android:scaleType="centerCrop"
    android:src="@drawable/grid" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/image_view_editor"
    android:textSize="28sp"
    android:textStyle="bold"
    android:textColor="@android:color/black"
    android:id="@+id/textView"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp" />


<Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/logo_black"
    android:scaleType="centerCrop"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.98" />

Image #1 (In Android Studio)

Android Studio

Image #2 (In the emulator)

Emulator

Upvotes: 0

Views: 752

Answers (8)

Mayank Garg
Mayank Garg

Reputation: 1304

I think your images size is too large so you should compress the image size then use in imageview.

 bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);

This code help to compress the long size file . 1. Firstly convert the image into bitmap . 2. apply this line on bitmap . 3. set bitmap data in imageview

Upvotes: 1

Ashif Ahamad
Ashif Ahamad

Reputation: 150

Kindly check you background image you used in your button. I think it is too large that throw exception at the time of drawing image of drawable at background of the button.

android:background="@drawable/logo_black" check this background image size

Upvotes: 1

azisuazusa
azisuazusa

Reputation: 317

How did you import your Image? Use Image Assets in Android Studio (right click in your Res directory and choose image asset) the image will be generated properly as needed

Upvotes: 5

Alishah momin
Alishah momin

Reputation: 114

Your background image is too large, you have to set to the screen size. Can you share the width and height of your background.

Upvotes: 1

Samuel Robert
Samuel Robert

Reputation: 11032

The drawable image you're using for the button is too large and it's about 100MB, Either reduce the size or use another drawable

Upvotes: 1

Upendra Shah
Upendra Shah

Reputation: 2301

Move your image in the (hi-res) drawable to drawable-xxhdpi. But in app development, you not need to use to large image its increase your APK file size.

or

The actual size of the image was too large, so compressed it.

Upvotes: 1

Reza.Abedini
Reza.Abedini

Reputation: 2257

check your error log:

java.lang.RuntimeException: Canvas: trying to draw too large(110250000bytes) bitmap.

your image is too large.one of these solutions will fix your problem 1-Use a library like Glide or Piccaso for loading your images. 2-if you putted your image in your drawable folder you can move it to drawable-xxxhdpi or drawable-xxhdpi

Upvotes: 1

phatnhse
phatnhse

Reputation: 3920

Your assets may be too large. Move your image in the (hi-res) drawable to drawable-xxhdpi. In this case, it could be your drawable/grid or drawable/logo_black

Read this article for more detail

Upvotes: 2

Related Questions