Activity已泄露窗口com.android.internal.policy

时间:2015-10-17 15:01:45

标签: android android-activity memory-leaks window

我使用下面的代码,我得到一个错误说,这个Activity已经泄露了窗口,我做错了什么。 这是我得到的错误,这是我正在使用的代码。 活动已泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView

   public class FullscreenActivity extends AppCompatActivity {

   String videourl;
   private Activity activity;
   private Context con;
   VideoView videoView;
   ProgressDialog progressDialog;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);

    videourl = getIntent().getExtras().getString("videoURL");

    activity = this;
    con = this;

    playVideo(videourl);


    }

    private void playVideo(String url){
    videoView = (VideoView) activity.findViewById(R.id.full_video);

    videoView.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(final MediaPlayer mp) {
            if (progressDialog != null) {
                progressDialog.cancel();
            }
        }
      });

       videoView.setOnErrorListener(new OnErrorListener() {

        public boolean onError(final MediaPlayer arg0, final int arg1,
                               final int arg2) {
            // TODO Auto-generated method stub

            if (progressDialog != null) {
                progressDialog.cancel();
            }

            return false;
         }
     });

     final MediaController mediaController = new MediaController(con);
     mediaController.show(1000);
     videoView.setMediaController(mediaController);

     if (isOnline()){

        try{
            playurl(url);
        }
        catch(Exception e) {
            Log.i("network problem:", e.toString());
        }
     }
     else {
        AlertMessage.showMessage(this, "Info", "No internet Connection");
        finish();
     }

   }

    private void playurl(String link) {
      try {progressDialog = ProgressDialog.show(FullscreenActivity.this, "Please wait...", "Loading...", false, false);
        link = URLEncoder.encode(link, "UTF-8");
        videoView.setVideoURI(Uri.parse(link));
        videoView.requestFocus();
        videoView.start();

        videoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(final MediaPlayer mp) {

                if (progressDialog != null) {
                    progressDialog.cancel();
                }

            }

        });

        videoView.setOnErrorListener(new OnErrorListener() {

            public boolean onError(final MediaPlayer arg0, final int arg1,
                                   final int arg2) {
                // TODO Auto-generated method stub

                if (progressDialog != null) {
                    progressDialog.cancel();
                }

                return false;
            }
        });

        videoView.setOnCompletionListener(new OnCompletionListener(){

            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                finish();
            }

        });
     }

     catch (UnsupportedEncodingException e){
        if (progressDialog != null) {
            progressDialog.cancel();
        }
        Toast.makeText(con, "can't play", Toast.LENGTH_SHORT).show();
        finish();
        e.printStackTrace();

     }

   }
   public boolean isOnline(){
   ConnectivityManager cm =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();

    if (ni != null && ni.isConnectedOrConnecting())
    {
        return true;
    }
    else{
        return false;
      }

      }

    }

//这是AlertMessage.java     公共类AlertMessage {

 public static void showMessage(final Context c, final String title,
        final String s) {
    final AlertDialog.Builder aBuilder = new AlertDialog.Builder(c);
    aBuilder.setTitle(title);
    aBuilder.setMessage(s);

    aBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(final DialogInterface dialog, final int which) {
            dialog.dismiss();
        }

    });

    aBuilder.show();
  }

  public static void showProgress(final Context c,
        ProgressDialog progressDialog) {
    if (progressDialog == null) {
        progressDialog = new ProgressDialog(c);
    }
    if (progressDialog != null && !progressDialog.isShowing()) {
        progressDialog = ProgressDialog.show(c, "Please wait...",
                "Buffering...", true, true);
    }

  }

  public static void cancelProgress(final Context c,
        final ProgressDialog progressDialog) {

    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();

    }
}

}

 11613-11613/com.Ippc.mobileapp E/WindowManager﹕ android.view.WindowLeaked:  Activity com.Ippc.mobileapp.FullscreenActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{2ca51238 V.E..... R.....I. 0,0-1002,567} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:465)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:277)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:312)
        at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
        at com.Ippc.mobileapp.utils.AlertMessage.showMessage(AlertMessage.java:25)
        at com.Ippc.mobileapp.FullscreenActivity.playVideo(FullscreenActivity.java:88)
        at com.Ippc.mobileapp.FullscreenActivity.onCreate(FullscreenActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:6283)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
        at android.app.ActivityThread.access$900(ActivityThread.java:177)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

0 个答案:

没有答案