0

I would like to play a movie at the begning of my Android application. So I use this code but it generates this error :

android.view.WindowLeaked:MainActivity has leaked window com.android.internal.policy

public class MainActivity extends AppCompatActivity {

private static int TIME_OUT = 8000;
String SrcPath = "android.resource://com.appdev.loicomelectronique/debut";
VideoView video;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(1);
    getWindow().setFlags(1024, 1024);
    setContentView(R.layout.activity_main);

    video = (VideoView)findViewById(R.id.videoView);
    video.setVideoURI(Uri.parse(this.SrcPath));
    video.requestFocus();
    video.start();
    new Handler().postDelayed(new Runnable()
    {
        public void run()
        {
            Intent localIntent = new Intent(MainActivity.this, MainActivityLoi.class);
            startActivity(localIntent);
            //MainActivity.this.finish();
        }
    }, TIME_OUT);
}

}

Please how can I fix it. All I found on web are about displaying a Dialog but in my case it is a video.

Thanks

Djen Nik
  • 63
  • 9
  • You need to destroy the handler and runnable before your activity pauses or destroys – Ak9637 Nov 17 '16 at 11:44
  • Okay, Thanks. But please, how can I make it ? I mean, destroy the handler and runnable befor my MainActivity pauses ? – Djen Nik Nov 17 '16 at 11:46
  • Create a reference to that handler then use: handler.removeCallbacksAndMessages(null); to remove all messages and callbacks see here: http://stackoverflow.com/questions/7407242/how-to-cancel-handler-postdelayed – sup4eli Nov 17 '16 at 11:52

2 Answers2

1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(1);
    getWindow().setFlags(1024, 1024);
    setContentView(R.layout.activity_main);

    video = (VideoView)findViewById(R.id.videoView);
    video.setVideoURI(Uri.parse(this.SrcPath));
    video.requestFocus();
    video.start();
    mHandler=new Handler();
    mRunable=new Runable()
    {
        public void run()
        {
            Intent localIntent = new Intent(MainActivity.this, MainActivityLoi.class);
            startActivity(localIntent);
            //MainActivity.this.finish();
        }
    };
    mhandler.postDelayed(mRunnable,TIME_OUT);
    
}
@Override
protected void onStop() {
     if(mHandler!=null){
         if(mHandler!=null){
                mHandler.removeCallbacks(mRunnable);
         }
         mHandler==null;
             
     }
     
     super.onStop();
        
       
 }
Ak9637
  • 990
  • 6
  • 12
0

Here is my layout.xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.appdev.loicomelectronique.activities.MainActivity">

<VideoView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/videoView"/>

 </FrameLayout>

I don't specify a dimension for video, just the layout and my .mp4 video file Thanks

Djen Nik
  • 63
  • 9