问题描述
我想为了学习Android开发创建一个简单的手电筒应用程序。我想,这样当您单击它时改变图像的光的ImageView对象得到它。但是现在它崩溃时,调试器到达light.setImageResource()。
进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.ImageView;
公共类闪存扩展ActionBarActivity { 关于= FALSE公共布尔; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_flash); ImageView的光=(ImageView的)findViewById(R.id.light); light.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
ImageView的光=(ImageView的)findViewById(R.id.light);
如果(上){
light.setImageResource(R.drawable.flash_off);
关于= FALSE;
}其他{
light.setImageResource(R.drawable.flash_on);
关于= TRUE;
}
}
});
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.menu_flash,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId(); // noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
} 返回super.onOptionsItemSelected(项目);
}
}
它引发错误是
---------崩溃的开始
01-18 14:05:27.675 1953年至1953年/ com.peterchappy.openflash E / AndroidRuntime:致命异常:主要
工艺:com.peterchappy.openflash,PID:1953
java.lang.OutOfMemoryError:无法分配与4194304免费字节51840012字节分配和13MB直到OOM
的 CRASH:的结果
当你的堆栈跟踪说: - 你试图显示在您的视图一个巨大的位图:java.lang中的的OutOfMemoryError 无法分配与4194304免费字节51840012字节分配和13MB,直到OOM。您的手机不喜欢它。结果
两种解决方案:
- 降低图像的尺寸,使用较小的图片,然后重试!
- 或set
的android:largeHeap =真正的
在清单
,在你的应用程序标签。它会增加你堆的大小,避免内存溢出错误,但您的应用程序可能会滞后。
的提示:的结果
你不需要,你的第二行重新找到的标识的看法。结果
尝试检索其ID与全球
变量作为后续,也可以使用 getResources()
方法,让您绘制:
公共类闪存扩展ActionBarActivity { ImageView的光; //初始化您的变量
关于= FALSE公共布尔; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_flash); //找到其ID与全局变量
光=(ImageView的)findViewById(R.id.light); light.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
如果(上){
//获取资源绘
light.setImageResource(
。getResources()getDrawable(R.drawable.flash_off));
关于= FALSE;
}其他{
light.setImageResource(
。getResources()getDrawable(R.drawable.flash_on));
关于= TRUE;
}
}
});
}
...
}
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
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 yourManifest
, 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;
}
}
});
}
...
}
这篇关于应用程序崩溃对ImageView的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!