App crashes on change of ImageView

后端 未结 2 1609
粉色の甜心
粉色の甜心 2021-01-24 09:14

I\'m trying to create a simple flashlight app in order to learn android development. I\'m trying to get it so that when when you click on the light ImageView object it changes t

相关标签:
2条回答
  • 2021-01-24 09:52

    CRASH:
    As your stacktrace says: "java.lang.OutOfMemoryError: Failed to allocate a 51840012 byte allocation with 4194304 free bytes and 13MB until OOM" - you try to display a huge bitmap on your view. Your phone doesn't like it.
    Two solutions:

    • reduce the dimensions of your image, use a smaller image... and retry!
    • or set android:largeHeap="true" in your Manifest, in your application tags. It will increase your heap size and avoid the OutOfMemory error, but your application will probably lag.

    TIPS:
    You don't need the second line where you re-find the id's view.
    Try to retrieve its id with a global variable as follow and also use getResources() method to get your drawable:

    public class flash extends ActionBarActivity {
    
        ImageView light; // init your variable
        public Boolean on = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_flash);
    
            // find its id with its global variable
            light = (ImageView) findViewById(R.id.light);
    
            light.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    if(on){
                        // get the resource drawable
                        light.setImageResource(
                            getResources().getDrawable(R.drawable.flash_off));
                        on = false;
                    }else{
                        light.setImageResource(
                            getResources().getDrawable(R.drawable.flash_on));
                        on = true;
                    }
                }
            });
        }
        ...
    }
    
    0 讨论(0)
  • 2021-01-24 10:02

    you do not need to declare a variable light in the handler

    public class flash extends ActionBarActivity {
    
            public Boolean on = false;
            ImageView light;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_flash);
    
                light = (ImageView) findViewById(R.id.light);
    
                light.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        if(on){
                            light.setImageResource(R.drawable.flash_off);
                            on = false;
                        }else{
                            light.setImageResource(R.drawable.flash_on);
                            on = true;
                        }
                    }
                });
            }
    
    0 讨论(0)
提交回复
热议问题