应用程序在更改ImageView时崩溃

我正在尝试创建一个简单的手电筒应用程序,以学习Android开发。 我试图得到它,以便当你点击光ImageView对象时,它会改变图像。 但是现在它在调试器到达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); } } 

它抛出的错误是

  --------- 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 

CRASH:
正如您的stacktrace所说:“java.lang.OutOfMemoryError 无法分配51840012字节分配4194304空闲字节和13MB直到OOM” – 您尝试在视图上显示一个巨大的位图。 你的手机不喜欢它。
两种解决方案

  • 缩小图像尺寸,使用较小的图像……然后重试!
  • 或者在你的Manifest ,在你的应用程序标签中设置android:largeHeap="true" 。 它会增加堆大小并避免OutOfMemory错误,但您的应用程序可能会滞后。

提示:
您不需要第二行,您可以在其中重新找到 ID的视图。
尝试使用global变量检索其id,如下所示,并使用getResources()方法获取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; } } }); } ... } 

您不需要在处理程序中声明变量light

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