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
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:
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;
}
}
});
}
...
}
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;
}
}
});
}