应用程序在android中关闭一段时间后关闭

时间:2014-03-13 07:29:09

标签: android

我创建了一个图像幻灯片放映类型应用程序,我的应用程序使用了线程。

我的应用程序运行成功,但一段时间后应用程序突然关闭。

我使用以下代码行来释放内存

System.gc();
Runtime.getRuntime().gc();

没有上述代码同样的问题。

logcat的:

03-13 12:45:09.250  /  dalvikvm:
@@@ ABORTING: DALVIK: HEAP MEMORY CORRUPTION IN internal_bulk_free addr=0x0
20713 20715 F03-13 12:45:09.250  /  libc
Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 20715 (GC) 

如何解决我的问题?

请帮帮我。

我的代码是:

    public class MyRunnable implements Runnable {

          private int delayTime = 0;
          private Vector<Integer> my_PlaylistRecord_ContentIds =new Vector<Integer>();
          private Vector<Integer> delayArray =new Vector<Integer>();

          private Vector<Long> video_Duration_List = new Vector<Long>();

          private Vector<Integer> relative_Ids = new Vector<Integer>();
          private Vector<RelativeLayout> layouts = new Vector<RelativeLayout>();


          private RelativeLayout customRelativeLayout ;

          private ArrayList<String> _idArray;
          private ArrayList<String> _delayArray;

          private int countPlus = 0;

          private int totalSize = 0;

          private ArrayList<Playlist_record> playlist_records;


          private FinalPlaylist finalPlaylist;

          private int fullscreenId =0;
          private int screenId =0;
          private int screenIndex =0;

          private long videoDuration  = 0;


          public MyRunnable(ArrayList<Integer> _playlistRrecord_ContentIds, FinalPlaylist _finalPlaylist, List<Integer> delayLists, List<Long> videoDurationList) {

              finalPlaylist = _finalPlaylist;

                for (Integer id : _playlistRrecord_ContentIds) {
                    //System.out.println("content id:"+id);
                    my_PlaylistRecord_ContentIds.add(id);
                }
                for (Integer string : delayLists) {
                    //System.out.println("delay time :"+string);
                    delayArray.add(string);
                }


                for (Long videoDuration : videoDurationList) {
                    video_Duration_List.add(videoDuration);
                }
              totalSize = delayArray.size();

              Iterator<Integer> myVeryOwnIterator = layoutMap.keySet().iterator();
                while(myVeryOwnIterator.hasNext()) {

                    int key=(Integer) myVeryOwnIterator.next();

                    System.out.println("key:"+key);

                        customRelativeLayout = layoutMap.get(key);
                        //System.out.println(" test customRelativeLayout.getChildCount():"+customRelativeLayout.getChildCount());
                        layouts.add(customRelativeLayout);
                        relative_Ids.add(key);
                }


                // Find screen s 



                 List<Container> containers = appDataBase.getAllCotainer();
                 for (Container container : containers) {

                    if(container.getName().equals("S")){
                            System.out.println("screen INVISIBLE index:");
                            screenId = container.getContainer_id();
                            System.out.println(" index :"+screenId);
                    }
                    if(container.getName().equals("FS")){
                        fullscreenId = container.getContainer_id();
                    }

               }

               // find index number of screen s container
                 for (int i = 0 ; i < relative_Ids.size() ; i++) {

                     if(screenId == relative_Ids.get(i)){
                         screenIndex = i;
                     }
                 }

                /* for (int i = 0 ; i < relative_Ids.size() ; i++) {

                 List<Container> containers = appDataBase.getAllCotainer();
                 for (Container container : containers) {

                     if(container.getName().equals("FS")){
                            System.out.println("Full screen INVISIBLE index:"+i);
                            RelativeLayout relativeLayout = layouts.get(i);
                            //relativeLayout.setVisibility(View.INVISIBLE);
                            layouts.set(i, relativeLayout);

                        } else if(container.getName().equals("S")){

                            System.out.println("screen INVISIBLE index:"+i);

                            RelativeLayout relativeLayout = layouts.get(i);
                            relativeLayout.setVisibility(View.INVISIBLE);
                            layouts.set(i, relativeLayout);

                    }

                }

            }*/

        }

        public void run() {

              System.out.println("screen container id:"+screenId);

              while(IS_THREAD_RUN){


                  if(countPlus < totalSize){

                    Runnable iRunnable= new Runnable() {
                          public void run() {



                        //    System.out.println(" ** countPlus:"+ countPlus);
                             // System.out.println(" ** totalSize:"+ totalSize);
                            //  System.out.println("my_playlist_ids size:"+my_PlaylistRecord_ContentIds.size());
                             // System.out.println("my_playlist_ids content:"+my_PlaylistRecord_ContentIds.get(countPlus));


                              // Get all playlist record

                              List<Playlist_record> playlist_records =  finalPlaylist.getPlayerArray();
                              Playlist_record playlist_record = playlist_records.get(countPlus);

                              //System.out.println("test run container id:"+playlist_record.getContainer_id());


                              // ***********************************
                              // Check playlist_record validation  
                              // ***********************************

                              //System.out.println("playlist_recordId:"+my_playlist_ids.get(countPlus));


                             boolean isvalidContent = false;
                             isvalidContent = appDataBase.isPlayContent(playlist_record.getPlaylist_record_id());
                             System.out.println("Valid Content:"+isvalidContent);

                             if(isvalidContent){ 
                             // Hide layout, Fetch all container data and get name equals to FS and S then relative layout 

                              /*
                                 for (int i = 0 ; i < relative_Ids.size() ; i++) {

                                     List<Container> containers = appDataBase.getAllCotainer();
                                     for (Container container : containers) {

                                         if(container.getName().equals("FS")){
                                                System.out.println("Full screen INVISIBLE index:"+i);
                                                RelativeLayout relativeLayout = layouts.get(i);
                                                //relativeLayout.setVisibility(View.INVISIBLE);
                                                layouts.set(i, relativeLayout);

                                            } else if(container.getName().equals("S")){

                                                System.out.println("screen INVISIBLE index:"+i);

                                                RelativeLayout relativeLayout = layouts.get(i);
                                                relativeLayout.setVisibility(View.INVISIBLE);
                                                layouts.set(i, relativeLayout);

                                        }

                                    }
                                 }*/

                                    //System.out.println(" layouts:"+layouts.toString());

                                    for (int i = 0 ; i < relative_Ids.size() ; i++) {

                                             if(relative_Ids.get(i) == playlist_record.getContainer_id()){
                                                  //System.out.println("countainer id:"+relative_Ids.get(i));
                                                // System.out.println("playlist_record.getContainer_id():"+playlist_record.getContainer_id());

                                                 customRelativeLayout = layouts.get(i);
                                                // System.out.println("customRelativeLayout.getChildCount():"+customRelativeLayout.getChildCount());

                                                 // Fetch all container data and get name equals to FS and S then relative layout visible


                                                    if(screenId == playlist_record.getContainer_id()){

                                                        customRelativeLayout.setVisibility(View.VISIBLE);
                                                        //customRelativeLayout.bringToFront();
                                                    }else {

                                                        RelativeLayout relativeLayout = layouts.get(screenIndex);
                                                        relativeLayout.setVisibility(View.INVISIBLE);
                                                        layouts.set(screenIndex, relativeLayout);
                                                    }

                                                    //Weather id match

                                                        try {
                                                            Feature_Defination feature_Defination = appDataBase.getFeatureDefinition(relative_Ids.get(i));
                                                            if(relative_Ids.get(i)==feature_Defination.getContainerID()){
                                                                    System.out.println("match");
                                                                    displayWeatherData(customRelativeLayout,feature_Defination.getContainerID(),feature_Defination);
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }


                                                 //System.out.println("transition effect:"+playlist_record.getTransition());

                                                 //customRelativeLayout.setVisibility(View.VISIBLE);

                                                   setContent(my_PlaylistRecord_ContentIds.get(countPlus),customRelativeLayout,getAnimation(playlist_record.getTransition()),delayArray.get(countPlus));



                                             }
                                    } // End for loop


                                     System.out.println("my_playlist_ids:"+my_PlaylistRecord_ContentIds.get(countPlus));

                             // setContent(my_playlist_ids.get(countPlus),customRelativeLayout);
                              countPlus =countPlus+1; 
                              if(countPlus >= totalSize){
                                  countPlus = 0;
                              }
                        }else{
                              countPlus =countPlus+2;
                              if(countPlus >= totalSize){
                                  countPlus = 0;
                              }
                        }


                        } //run method end


                     };

                     handler.post(iRunnable);

                  }else{
                      countPlus = 0;
                  }

                  //System.out.println("countplus before thread sleep:"+delayArray.get(countPlus)+" countPlus:"+countPlus);
                  //System.out.println("countplus sleep  video duration:"+videoDuration);

                 try {  

                          long sleepTime = delayArray.get(countPlus);
                         // System.out.println("sleep delay :"+sleepTime);
                          if(sleepTime==0){

                            long videoTime = video_Duration_List.get(countPlus);
                            if(videoTime == 0 ){
                                sleepTime = 1000;
                            }else{
                                sleepTime = videoTime;
                            }
                          }else{
                             sleepTime = (sleepTime * 1000)+1000;

                          }
                          /*
                          if(videoDuration!=0){
                              System.out.println("sleep count plus"+countPlus);
                              countPlus = countPlus - 1;
                              if(countPlus<=0){
                                  countPlus = 0;
                              }
                              System.out.println("sleep countPlus:"+countPlus);
                             sleepTime = videoDuration;
                          }*/
                         // new DisplayContentAsync().execute(""+my_PlaylistRecord_ContentIds.get(countPlus));

                         System.out.println("sleep time:"+sleepTime);
                         Thread.sleep(sleepTime);


                          //Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
              } // run method end

          }
        }

/*    class DisplayContentAsync extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {



                     // get video file length
                     // check video


            // *********************************************************



                         String play_FileName = "";

                            //Get all record from content table
                               File conFile = new File( Util.ROOT_PATH + "Contents/"+play_FileName);
                                //Check image file
                                ImageFileFilter imageFileFilter = new ImageFileFilter(conFile);
                                VideoFileFilter videoFileFilter = new VideoFileFilter(conFile);


                                Content content = appDataBase.getContentTemp(""+params[0]);

                                System.out.println("content id:"+content.getContent_id());
                                System.out.println("content path:"+content.getContent());

                                try {
                                    String[] contentArray = null;
                                    contentArray = content.getContent().split("/");
                                    play_FileName = contentArray[contentArray.length-1];
                                } catch (Exception e2) {
                                    // TODO Auto-generated catch block
                                    e2.printStackTrace();
                                }
                                String filePath = Util.ROOT_PATH + "Contents/"+conFile.getName();
                                if(videoFileFilter.accept(conFile)){
                                      System.out.println("Video file name:"+play_FileName);
                                    // Get video file play duration
                                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                                    retriever.setDataSource(filePath);
                                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                                    long time1 = Long.parseLong( time );;
                                    System.out.println("video time: "+time1);
                                }


                     // setContent(my_playlist_ids.get(countPlus),customRelativeLayout);




            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        }
     }
*/
    @SuppressLint("NewApi")
    public void setContent(int content_id,RelativeLayout _customRelativeLayout,List<Animation> _animations, int _videoTimeDuration){
        //System.gc();
        //Runtime.getRuntime().gc();

        List<Animation> animations = _animations;

        RelativeLayout customRelativeLayout = _customRelativeLayout;
        ImageView imageView = null;
        VideoView videoView = null;
        WebView webView = null;

        int height = customRelativeLayout.getHeight();
        int width = customRelativeLayout.getWidth();
        final int videoTimeDuration = _videoTimeDuration;

        Animation anim1 = null ;
        Animation anim2 = null;
        Animation anim3 = null;

        for(int p=0; p<animations.size();p++){

            if(p==0){
                anim1 = animations.get(p);
                System.out.println("animation 1");
            }else if(p==1){
                anim2 = animations.get(p);
                System.out.println("animation 2");
            }else if(p==2){
                anim3 = animations.get(p);
                System.out.println("animation 3");
            }
        }


        //System.out.println("layout height :"+height);
        //System.out.println("layout height :"+height);
        // Find all child from relative layout
        int childcount = customRelativeLayout.getChildCount();
        //System.out.println("get all child:"+childcount);
        for (int i=0; i < childcount; i++){
              View view = customRelativeLayout.getChildAt(i);
              if (view instanceof ImageView) {
                    imageView = (ImageView) view;
                    // do what you want with imageView
                }else if (view instanceof VideoView) {
                    videoView = (VideoView) view;
                    // do what you want with imageView
                }else if (view instanceof WebView) {
                    webView = (WebView) view;
                    // do what you want with imageView
                }

        }
        String play_FileName = "";

        //Get all record from content table

            //System.out.println("content id match with:"+content_id);
            Content content = appDataBase.getContent(""+content_id);

            //System.out.println("content id:"+content.getContent_id());
            //System.out.println("content path:"+content.getContent());

            try {
                String[] contentArray = null;
                contentArray = content.getContent().split("/");
                play_FileName = contentArray[contentArray.length-1];
            } catch (Exception e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }


           System.out.println("Play file name:"+play_FileName);
           //Toast.makeText(Display.this, play_FileName, 1000).show();

         //System.out.println("Play file duration:"+duration);
        /*
           // Set animation

            if(animations!=null && animations.size()>=2){
                System.out.println("animation 2 available ");



                final Animation endanim = anim2;
                final RelativeLayout relativeLayout = customRelativeLayout;

                customRelativeLayout.startAnimation(anim1);
                anim1.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation arg0) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation arg0) {

                    }

                    @Override
                    public void onAnimationEnd(Animation arg0) {
                        relativeLayout.startAnimation(endanim);

                    }
                });
            }else if(animations!=null && animations.size()==1){
                System.out.println("animation 1 available");
                imageView.setAnimation(anim1);
            }else {
                System.out.println("animation not available");
            }
        */
                File conFile = new File( Util.ROOT_PATH + "Contents/"+play_FileName);
                //Check image file
                ImageFileFilter imageFileFilter = new ImageFileFilter(conFile);
                VideoFileFilter videoFileFilter = new VideoFileFilter(conFile);
                WebFileFilter webFileFilter = new WebFileFilter(conFile);

                String filePath = Util.ROOT_PATH + "Contents/"+conFile.getName();
                //check file size is zero or not
                File chkFile = new File(filePath);
                if(chkFile.length()>0){
                    if(imageFileFilter.accept(conFile)){
                        //System.out.println("filter image file name:"+conFile.getName());
                        videoView.setVisibility(View.GONE);
                        imageView.setVisibility(View.VISIBLE);
                        webView.setVisibility(View.GONE);



                        // Check android os 
                        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                        if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
                            //System.out.println("jelly bean");
                            Drawable drawable = Drawable.createFromPath(filePath);
                            imageView.setBackground(drawable); 
                            //imageView.setImageBitmap(decodeFile(chkFile, 1000, 1000));

                            // Set animation

                            if(animations!=null && animations.size()>=2){
                                System.out.println("animation 2 available ");



                                final Animation endanim = anim2;
                                final ImageView finalImage = imageView;

                                imageView.startAnimation(anim1);
                                anim1.setAnimationListener(new AnimationListener() {

                                    @Override
                                    public void onAnimationStart(Animation arg0) {

                                    }

                                    @Override
                                    public void onAnimationRepeat(Animation arg0) {

                                    }

1 个答案:

答案 0 :(得分:0)

你可以尝试这个。在android清单文件中添加以下行:

android:largeHeap="true"

感谢。