Error while trying to add an image as a button background

Asked

Viewed 260 times

0

A few days ago, Android Studio indicated that it had updates that were available...

I had already finished an application some time ago and it was ready to be published and Talz, only lacked the license of Google Play that I needed to buy.

I bought the license and returned to Android Studio to implement ads (adMob) and generate the key.

I invented implementing the build update Gradle (it was "optional") in the app and immediately it stopped working...

So I was redoing it until I needed to add backgrounds to the application’s Uttons, just like I did on the other and for some reason the app doesn’t start and no errors are charged other than this:

07-01 23:40:09.925 6690-6690/genesysgeneration.ruleoftree E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: genesysgeneration.ruleoftree, PID: 6690
                                                                            java.lang.RuntimeException: Canvas: trying to draw too large(146963896bytes) 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:528)
                                                                                at android.view.View.getDrawableRenderNode(View.java:17428)
                                                                                at android.view.View.drawBackground(View.java:17364)
                                                                                at android.view.View.draw(View.java:17176)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16167)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.draw(View.java:17188)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16167)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16162)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16162)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16162)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16162)
                                                                                at android.view.View.draw(View.java:16951)
                                                                                at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                                                                                at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                                                                                at android.view.View.draw(View.java:17188)
                                                                                at com.android.internal.policy.DecorView.draw(DecorView.java:753)
                                                                                at android.view.View.updateDisplayListIfDirty(View.java:16167)
                                                                                at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
                                                                                at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
                                                                                at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
                                                                                at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
                                                                                at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2608)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2215)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                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:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

inserir a descrição da imagem aqui

Without the implementation of backgrounds the same works, is to add any image and it no longer works.

Part of Activity code:

package genesysgeneration.ruleoftree;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

public class MainActivity01 extends AppCompatActivity implements View.OnClickListener{

    private EditText et01, et02, et03;
    private TextView tv01/*, tvTest*/;
    private double l01, l02, l03, equalizer, lxx;
    private ImageView btnChange01, btnCompras, btnMoeda, btnInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main01);

        tv01=(TextView)findViewById(R.id.tv01);
        tv01.setText(String.valueOf(0));

        l01=0;
        l02=0;
        l03=0;
        lxx=0;

        et01=(EditText)findViewById(R.id.et01);
        et02=(EditText)findViewById(R.id.et02);
        et03=(EditText)findViewById(R.id.et03);

        addValuesDouble();

        btnChange01=(ImageView)findViewById(R.id.btnChange01);
        Picasso.with().load(R.drawable.setas_03).into(btnChange01);
        btnChange01.setOnClickListener(this);

        btnCompras=(ImageView)findViewById(R.id.btnCompras);
        btnCompras.setOnClickListener(this);

        btnMoeda=(ImageView)findViewById(R.id.btnMoeda);
        btnMoeda.setOnClickListener(this);

        btnInfo=(ImageView)findViewById(R.id.btnInfo);
        btnInfo.setOnClickListener(this);

    }
    
}

1 answer

1


You seem to be loading a very large image on the button (by the error message). Why not swap the button for an imageView and use Picasso to upload the image anyway? It does all the decoding of the image to load it on the screen.

Installs in Gradle:

compile 'com.squareup.picasso:picasso:2.5.2'

And use in the code:

Picasso.with(context).load(R.drawable.image).into(imageView);
  • Could you give me an example of doing this with a button? If I need to put Activity code in the post.

  • You don’t need a button. Use a same View image and arrow a Clicklistener in it to do what the button would do.

  • What I put in that 'context'?

  • Picasso.with(getApplicationContext()).load(R.drawable.setas_03).into(btnChange01); was this way, but the app for.

  • I imagine that Picasso does not work for buttons. As I said, you can use in Imageviews and make them work as buttons. In addition, you can use Picasso with other Imageviews that upload images. For example, the background image. It wouldn’t be too big?

  • I switched the Buttons for imageViews and added the Clicklistener, only thing that gives error is the space where I need to insert the context.

  • Usa Picasso.with(this).

  • Picasso.with(this).load(R.drawable.setas_03).into(btnChange01); gives error. Any other suggestion?

  • What error does it make? This btnChange01 is an imageView?

  • Yes it is. Error => FATAL EXCEPTION: main
 Process: genesysgeneration.ruleoftree, PID: 4321
 java.lang.RuntimeException: Canvas: trying to draw too large(146963896bytes) bitmap.

  • But this error doesn’t seem to be related to Picasso. Checked which line was pointed out by the error?

  • Look, I just found out the bug. The image was too big and Androidstudio was not being able to resize it when running the app. I used the common way to treat imageView, but I intend to test Picasso later.

  • 1

    But Picasso does just the treatment of these large images and loads in Imageview. So I suggested to use it for all the images in your app.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.