Reputation: 1179
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 the image. However right now it crashes when the debugger gets to light.setImageResource().
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class flash extends ActionBarActivity {
public Boolean on = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash);
ImageView light = (ImageView) findViewById(R.id.light);
light.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
ImageView light = (ImageView) findViewById(R.id.light);
if(on){
light.setImageResource(R.drawable.flash_off);
on = false;
}else{
light.setImageResource(R.drawable.flash_on);
on = true;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_flash, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The error it's throwing is
--------- beginning of crash
01-18 14:05:27.675 1953-1953/com.peterchappy.openflash E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.peterchappy.openflash, PID: 1953
java.lang.OutOfMemoryError: Failed to allocate a 51840012 byte allocation with 4194304 free bytes and 13MB until OOM
Upvotes: 1
Views: 2635
Reputation: 11978
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;
}
}
});
}
...
}
Upvotes: 2
Reputation: 1181
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;
}
}
});
}
Upvotes: 0