崩溃:
正如您的堆栈跟踪所说:“ java.lang。OutOfMemoryError :
未能分配51840012字节分配,其中包含4194304可用字节和13MB直到OOM”-您尝试在视图上显示巨大的位图。您的手机不喜欢它。
两种解决方案:
- 缩小图像尺寸,使用较小的图像…然后重试!
- 或
android:largeHeap="true"
在您的Manifest
,您的应用程序标记中进行设置。它将增加您的堆大小并避免OutOfMemory错误,但是您的应用程序可能会滞后。
提示:
您不需要在第二行中 重新查找 ID的视图。
尝试
global按照以下方式使用变量来获取其ID,并使用
getResources()method来获取可绘制对象:
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; } } }); } ...}


