0

MainActivity我导航playButtonPlayActivity然后我通过PlayActivity并且当i足够大时我去EndActivity但是我得到:

06-05 08:58:15.139: D/GestureDetector(15587): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-05 08:58:15.189: D/dalvikvm(15587): GC_FOR_ALLOC freed 1781K, 9% free 55209K/60039K, paused 14ms, total 14ms
06-05 08:58:15.199: I/dalvikvm-heap(15587): Grow heap (frag case) to 58.303MB for 3686416-byte allocation
06-05 08:58:15.234: D/dalvikvm(15587): GC_CONCURRENT freed <1K, 8% free 58809K/63687K, paused 12ms+4ms, total 36ms
06-05 08:58:15.264: D/dalvikvm(15587): GC_FOR_ALLOC freed 0K, 8% free 58809K/63687K, paused 15ms, total 15ms
06-05 08:58:15.264: I/dalvikvm-heap(15587): Forcing collection of SoftReferences for 14745616-byte allocation
06-05 08:58:15.284: D/dalvikvm(15587): GC_BEFORE_OOM freed 9K, 8% free 58799K/63687K, paused 20ms, total 20ms
06-05 08:58:15.284: E/dalvikvm-heap(15587): Out of memory on a 14745616-byte allocation.
06-05 08:58:15.284: I/dalvikvm(15587): "main" prio=5 tid=1 RUNNABLE
06-05 08:58:15.284: I/dalvikvm(15587):   | group="main" sCount=0 dsCount=0 obj=0x41241508 self=0x40fb69a0
06-05 08:58:15.284: I/dalvikvm(15587):   | sysTid=15587 nice=0 sched=0/0 cgrp=apps handle=1074585392
06-05 08:58:15.284: I/dalvikvm(15587):   | schedstat=( 942955652 175290385 911 ) utm=73 stm=20 core=3
06-05 08:58:15.284: I/dalvikvm(15587):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

这可能与Canvas但不确定。

我通过以下方式开始 playActivity:

    playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, PlayActivity.class);
                startActivity(intent);          
            }

        });

playActivity 看起来像这样,我不知道我想纠正什么:

package com.example.szpieg2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class PlayActivity extends Activity {
ArrayList<Info> listOfImages;
MyImageView upperIV, lowerIV;
int hits, i, cX, cY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_play);
        hits = 0; i = 0;
        load();
        //zaladowane obrazki dzial super
        play();
    }



    public void displayImages(){
        if (i * 2 + 1 < listOfImages.size()) {
            upperIV = (MyImageView) findViewById(R.id.upperImageView);
            upperIV.setImageResource(listOfImages.get(i * 2).image);
            lowerIV = (MyImageView) findViewById(R.id.lowerImageView);
            lowerIV.setImageResource(listOfImages.get(i * 2 + 1).image);
        }else{

        }
    }

    public void play(){
        displayImages();

        OnTouchListener otl = new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // Finger placed on screen
                    int x  = (int) event.getX();
                    int y  = (int) event.getY();
                    Log.i("x,y" ,"("+x + "," + y +")" );
                    Coordinates current = new Coordinates(x, y);
                    Info info = listOfImages.get(i*2);
                    if(checkTheHit(info, current)){
                        lowerIV.addOval(new MyOval(cX, cY));
                        upperIV.addOval(new MyOval(cX, cY));
                        hits++;
                        TextView txt = (TextView) findViewById(R.id.scoreTextView);
                        txt.setText("Points " + hits);
                        if(allHit(info)){
                            i++;
//                          try{Thread.sleep(500);}catch(Exception e){e.printStackTrace();}
                            displayImages();
                            lowerIV.clear();
                            upperIV.clear();
                        }

                    }

                }
                return false;
            }
        };

        upperIV.setOnTouchListener(otl);
        lowerIV.setOnTouchListener(otl);


    }


    public boolean allHit(Info info){
        for(Coordinates coordinates : info.listOfCoordinates)
            if(!coordinates.hit)
                return false;
        return true;
    }
    public boolean checkTheHit(Info info, Coordinates c){
        for(Coordinates coordinates : info.listOfCoordinates)
            if(coordinates.equals(c)){
                cX = coordinates.x;
                cY = coordinates.y;
                return true;
            }

        return false;
    }

    public void load(){
        try{
        listOfImages = new ArrayList<Info>();
        ArrayList<Coordinates> listOfCoordinates;
        Field[] fields=R.raw.class.getFields();
        for(int i = 0; i < fields.length; i++){//read every textFile and pair it with the image
            //image: v[versionNumber]i[name of image] example v2i2
            //info: info[name of described image]
            Log.i("tu", "tu");
            int resourceID = fields[i].getInt(fields[i]);
            InputStream is =  getResources().openRawResource(resourceID);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            String[] splittedLine;
            listOfCoordinates = new ArrayList<Coordinates>();
            while((line = br.readLine()) != null){
                splittedLine = line.split(" ");
                int x = Integer.parseInt(splittedLine[0]);
                int y = Integer.parseInt(splittedLine[1]);
                Coordinates c = new Coordinates(x, y);
                listOfCoordinates.add(c);
            }
            Log.i("loaded coordinates", listOfCoordinates.toString());
            //looking for image
            String image1Name = "v1i" + fields[i].getName().substring(fields[i].getName().length()-1);
            String image2Name = "v2i" + fields[i].getName().substring(fields[i].getName().length()-1);
            int id1 = getResources().getIdentifier(image1Name,"drawable", getPackageName()); 
            int id2 = getResources().getIdentifier(image2Name,"drawable", getPackageName()); 
            Log.i("id1", id1 + "");
            Log.i("id2", id2 + "");
            listOfImages.add(new Info(id1, listOfCoordinates));
            listOfImages.add(new Info(id2, listOfCoordinates));
            //dotad dziala
        }
        }catch(Exception e){e.printStackTrace();}

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_play, menu);
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            upperIV.clear();
            lowerIV.clear();
            i = 0;
            hits = 0;
            cX = cY = i = hits = 0;
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
4

1 回答 1

0

它说“E / dalvikvm-heap(15587):14745616字节分配的内存不足。” 也许这是你的问题?看来您分配了太多位图资源。

这是一篇很好的文章

于 2013-06-05T07:36:05.630 回答