在我的应用程序中,当我将我的 apk 文件从 eclipse 部署到我的选项卡时,它会显示以下错误。
06-20 18:32:52.550: E/AndroidRuntime(2927): FATAL EXCEPTION: main
06-20 18:32:52.550: E/AndroidRuntime(2927): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.Bitmap.nativeCreate(Native Method)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.content.res.Resources.loadDrawable(Resources.java:1709)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.content.res.Resources.getDrawable(Resources.java:581)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.content.res.Resources.loadDrawable(Resources.java:1694)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.content.res.Resources.getDrawable(Resources.java:581)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.view.View.setBackgroundResource(View.java:7533)
06-20 18:32:52.550: E/AndroidRuntime(2927): at Newtons.law.Newton_LawsActivity$1$1.run(Newton_LawsActivity.java:79)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.os.Handler.handleCallback(Handler.java:587)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.os.Looper.loop(Looper.java:123)
06-20 18:32:52.550: E/AndroidRuntime(2927): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-20 18:32:52.550: E/AndroidRuntime(2927): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 18:32:52.550: E/AndroidRuntime(2927): at java.lang.reflect.Method.invoke(Method.java:507)
06-20 18:32:52.550: E/AndroidRuntime(2927): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-20 18:32:52.550: E/AndroidRuntime(2927): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-20 18:32:52.550: E/AndroidRuntime(2927): at dalvik.system.NativeStart.main(Native Method)
然后我是goolge它我得到了一个解决方案......
@Override
protected void onDestroy() {
super.onDestroy();
//unbindDrawables(findViewById(R.id.rl1));
System.gc();
Runtime.getRuntime().gc();
}
我的整个班级代码如下.. 编辑
public class Newton_LawsActivity extends Activity implements OnTouchListener, OnGestureListener,OnDoubleTapListener {
/** Called when the activity is first created. */
OnTouchListener l;
ImageView animation;
ImageView hideimage;
TextView t,s;
Thread timer;
int timesleep=0;
int rid;
int passid;
int pausecount=0;
double pixel,pixel1,pixel2,pixel3;
String law="null";
private GestureDetector gestureDetector;
AnimationDrawable newtonAnimation;
int worm_big_count=0;
int apple_big_count=0;
float x,y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this);
// the frame-by-frame animation defined as a xml file within the drawable folder
animation = (ImageView)findViewById(R.id.image);
//animation.setBackgroundResource(R.drawable.anatomy_5);
hideimage=(ImageView)findViewById(R.id.imagein);
s=(TextView) findViewById(R.id.t);
t=(TextView) findViewById(R.id.text);
//Starting the first animation.
passid=R.drawable.firstlaw_00;
AnimationStart(passid,timesleep);
rid=R.drawable.touchnewtonfinal415;
law="firstlaw_01";
animation.setOnTouchListener(this);
registerForContextMenu(animation);
}
public void AnimationStart(final int id,final int time){
timer=new Thread(){
@Override
public void run(){
try{
Thread.sleep(time);
}catch(Exception e){}
finally{
Newton_LawsActivity.this.runOnUiThread(new Runnable() {
public void run(){
animation.setBackgroundResource(id);
newtonAnimation = (AnimationDrawable) animation.getBackground();
newtonAnimation.start();
}});
}
}
};
timer.start();
}
private void animationswitch(float x, float y) {
// TODO Auto-generated method stub
pixel = getmaskpixel(x,y,animation,rid);
pixel1= getmaskpixel(x+2,y,animation,rid);
pixel2= getmaskpixel(x,y+2,animation,rid);
pixel3= getmaskpixel(x+2,y+2,animation,rid);
Log.w("DEBUG","which is null:pixel" + pixel +"pixel1"+pixel1+"pixel2"+pixel2+"pixel3"+pixel3 );
if ((pixel==-583672 || pixel1==-583672 || pixel2==-583672 || pixel3==-583672 )&& law=="firstlaw_01"){
passid=R.drawable.firstlaw_01; //passid is referring animation file
law="firstlaw_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("Newton's first law says");
}else if(((pixel==-1.6711903E7 || pixel1==-1.6711903E7|| pixel2==-1.6711903E7 || pixel3==-1.6711903E7 )||(pixel==-1.6187607E7 || pixel1==-1.6187607E7|| pixel2==-1.6187607E7 || pixel3==-1.6187607E7)) && law=="firstlaw_01"){
passid=R.drawable.secondlaw_01;
law="secondlaw_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("Newton's Second law says");
}else if(((pixel==-1.6761609E7 || pixel1==-1.6761609E7|| pixel2==-1.6761609E7 || pixel3==-1.6761609E7 )||(pixel==-1.6760329E7 || pixel1==-1.6760329E7|| pixel2==-1.6760329E7 || pixel3==-1.6760329E7)||(pixel==-1.6236033E7 || pixel1==-1.6236033E7|| pixel2==-1.6236033E7 || pixel3==-1.6236033E7)||(pixel==-1.6236033E7 || pixel1==-1.6236041E7|| pixel2==-1.6236041E7 || pixel3==-1.6236041E7)) && law=="firstlaw_01"){
passid=R.drawable.thirdlaw_01; //passid is referring animation file
law="thirdlaw_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("Newton's Third law says");
}else if((pixel==-1 || pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="firstlaw_03"){
passid=R.drawable.firstlaw_03; //passid is referring animation file
law="firstlaw_04"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-65536 || pixel1==-65536 || pixel2==-65536 || pixel3==-65536)&& law=="firstlaw_05"){
passid=R.drawable.firstlaw_05; //passid is referring animation file
law="firstlaw_06"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-1.2444648E7|| pixel1==-1.2444648E7 || pixel2==-1.2444648E7 || pixel3==-1.2444648E7 ) && law=="firstlaw_07"){
passid=R.drawable.firstlaw_07; //passid is referring animation file
law="firstlaw_08"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-1 || pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="secondlaw_01"){
passid=R.drawable.secondlaw_01;
law="secondlaw_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("Newton's Second law says");
}else if((pixel==-1 || pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="secondlaw_03"){
passid=R.drawable.secondlaw_03; //passid is referring animation file
law="secondlaw_04"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("");
}else if((pixel==-1 || pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="secondlaw_05"){
passid=R.drawable.secondlaw_05; //passid is referring animation file
law="secondlaw_06"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-1 || pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="secondlaw_07"){
passid=R.drawable.thirdlaw_01; //passid is referring animation file
law="thirdlaw_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
t.setText("Newton's Third law says");
}else if((pixel==-65536 || pixel1==-65536 || pixel2==-65536 || pixel3==-65536 ) && law=="secondlaw_07"){
if(apple_big_count==0)
apple_big_count++;
else
apple_big_count--;
wormappleanimation();
animationstop_01();
}else if((pixel==-1.2444648E7 || pixel1==-1.2444648E7 || pixel2==-1.2444648E7 || pixel3==-1.2444648E7 ) && law=="secondlaw_07"){
if(worm_big_count==0)
worm_big_count++;
else
worm_big_count--;
wormappleanimation();
animationstop_01();
}else if(((pixel==-1.2444648E7 || pixel1==-1.2444648E7|| pixel2==-1.2444648E7 || pixel3==-1.2444648E7 )||((pixel==-9753559 || pixel1==-9753559|| pixel2==-9753559 || pixel3==-9753559))) && law=="thirdlaw_03"){
passid=R.drawable.thirdlaw_03; //passid is referring animation file
law="thirdlaw_04"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-65536|| pixel1==-65536 || pixel2==-65536 || pixel3==-65536 ) && law=="thirdlaw_05"){
passid=R.drawable.thirdlaw_05; //passid is referring animation file
law="thirdlaw_06"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}else if((pixel==-1|| pixel1==-1 || pixel2==-1 || pixel3==-1 ) && law=="lawmenu"){
animation.showContextMenu();
}
s.setText(Double.toString(pixel));
}
private void wormappleanimation() {
// TODO Auto-generated method stub
if(worm_big_count==0 && apple_big_count==0){
passid=R.drawable.secondlaw_07_wormapplesmall_01; //passid is referring animation file
law="secondlaw07_wormapplesmall_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}
else if(apple_big_count==1 && worm_big_count==0){
passid=R.drawable.secondlaw_07_applebig_01; //passid is referring animation file
law="secondlaw_07_applebig_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}
else if(apple_big_count==0 && worm_big_count==1){
passid=R.drawable.secondlaw_07_wormbig_01; //passid is referring animation file
law="secondlaw_07_wormbig_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}
else if(apple_big_count==1 && worm_big_count==1){
passid=R.drawable.secondlaw_07_wormapplebig_01; //passid is referring animation file
law="secondlaw_07_wormapplebig_02"; //changing the law string
timesleep=0; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
animationstop_01();
}
s.setText(Float.toString(x)+"-x"+Float.toString(y)+"-y"+Double.toString(pixel)+"-pixcolo");
}
private double getmaskpixel(float x, float y, ImageView view,int rid) {
// TODO Auto-generated method stub
Bitmap bm = BitmapFactory.decodeResource(getResources(), rid);
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) view.getWidth()) / width;
float scaleHeight = ((float) view.getHeight()) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(90);
// recreate the new Bitmap
Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height,matrix, false);
Log.w("DEBUG","which is null:image " + bitmap.getWidth() + " OR " +bitmap.getHeight() );
double bmWidth = view.getWidth();
double bmHeight = view.getHeight();
if ( x < 0 || y < 0 || x > bitmap.getWidth() || y > bitmap.getHeight()){
return 0; //Invalid, return 0
}else{
//Convert touched x, y on View to on Bitmap
int xBm = (int)(x * (bmWidth / bitmap.getWidth()));
int yBm = (int)(y * (bmHeight / bitmap.getHeight()));
return bitmap.getPixel((int)xBm,(int) yBm);
}
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
// TODO Auto-generated method stub
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return true;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onDoubleTap(MotionEvent e) {
x=e.getX();
y= e.getY();
Log.w("DEBUG","which is null:image " + this + " OR " +animation);
if(pausecount==0){
pausecount++;
}else {
pausecount--;
}
pause(pausecount);
return true;
}
public void pause(int pausecount) {
// TODO Auto-generated method stub
if(pausecount==1){
hideimage.setVisibility(0);
try {
synchronized (animation) {
animation.wait(1000);
}
}catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pause(pausecount);
}else{
hideimage.setVisibility(1);
animation.notify();
}
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y= e.getY();
Log.w("DEBUG","which is null:image " + x + " OR " +y);
animationswitch(x, y);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Log.w("debug", "MENU pressed");
animation.showContextMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}
void animationstop_01(){
if(law=="firstlaw_02"){
hideimage.setBackgroundResource(R.drawable.touchnewtonfinal415_282);
hideimage.invalidate();
rid=R.drawable.touchnewtonfinal415_282;
Log.w("debug","it is firstlaw_02");
passid=R.drawable.firstlaw_02; //pass id is referring animation file
law="firstlaw_03"; //changing the law string
timesleep=7750; //there is no time for sleeping
AnimationStart(passid,timesleep); //passing the value
}else if(law=="firstlaw_04"){
hideimage.setBackgroundResource(R.drawable.touchnewtonfinal415_282);
hideimage.invalidate();
rid=R.drawable.touchnewtonfinal415_282;
Log.w("debug","it is firstlaw_04");
passid=R.drawable.firstlaw_04; //pass id is referring animation file
law="firstlaw_05"; //changing the law string
timesleep=22500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="firstlaw_06"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_586);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_586;
Log.w("debug","it is firstlaw_06");
passid=R.drawable.firstlaw_06; //passid is referring animation file
law="firstlaw_07"; //changing the law string
timesleep=6250; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="firstlaw_08"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_737);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_737;
Log.w("debug","it is firstlaw_08");
passid=R.drawable.firstlaw_08; //passid is referring animation file
law="secondlaw_01"; //changing the law string
timesleep=4000; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_737);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_737;
Log.w("debug","it is secondlaw_02");
passid=R.drawable.secondlaw_02; //passid is referring animation file
law="secondlaw_03"; //changing the law string
timesleep=7000; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_04"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_986);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_986;
Log.w("debug","it is secondlaw_04");
passid=R.drawable.secondlaw_04; //passid is referring animation file
law="secondlaw_05"; //changing the law string
timesleep=6500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_06"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1059);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1059;
Log.w("debug","it is secondlaw_06");
passid=R.drawable.secondlaw_06; //passid is referring animation file
law="secondlaw_07"; //changing the law string
timesleep=1500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_07_wormapplebig_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1161_01);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1161_01;
Log.w("debug","it is secondlaw_07_wormapplebig_02");
passid=R.drawable.secondlaw_07_wormapplebig_02; //passid is referring animation file
law="secondlaw_07"; //changing the law string
timesleep=1500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_07_wormbig_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1199_01);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1199_01;
Log.w("debug","it is secondlaw_07_wormbig_01");
passid=R.drawable.secondlaw_07_wormbig_02; //pass id is referring animation file
law="secondlaw_07"; //changing the law string
timesleep=1500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw_07_applebig_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1128_01);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1128_01;
Log.w("debug","it is secondlaw_07_applebig_02");
passid=R.drawable.secondlaw_07_applebig_02; //pass id is referring animation file
law="secondlaw_07"; //changing the law string
timesleep=1500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="secondlaw07_wormapplesmall_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1093);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1093;
Log.w("debug","it is secondlaw07_wormapplesmall_02");
passid=R.drawable.secondlaw_07_wormapplesmall_02; //pass id is referring animation file
law="secondlaw_07"; //changing the law string
timesleep=1500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="thirdlaw_02"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1322);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1322;
Log.w("debug","it is thirdlaw_02");
passid=R.drawable.thirdlaw_02; //passid is referring animation file
law="thirdlaw_03"; //changing the law string
timesleep=7250; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="thirdlaw_04"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1690);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1690;
Log.w("debug","it is thirdlaw_04");
passid=R.drawable.thirdlaw_04; //passid is referring animation file
law="thirdlaw_05"; //changing the law string
timesleep=16500; //there is no time for sleeping
AnimationStart(passid,timesleep);
}else if(law=="thirdlaw_06"){
hideimage.setBackgroundResource(R.drawable.newtonfinal415_1931);
hideimage.invalidate();
rid=R.drawable.newtonfinal415_1931;
Log.w("debug","it is thirdlaw_06");
passid=R.drawable.thirdlaw_06; //passid is referring animation file
law="lawmenu"; //changing the law string
timesleep=16750; //there is no time for sleeping
AnimationStart(passid,timesleep);
}
已编辑