开发者

Leaked Window when dialog button clicked

I'm trying to show an about page to the users of my wallpaper app when they click the about button however i get leaked window errors in a log cat and the activity quits before it shows the dialog.

Here is the code:

 /*
  *
  * Sensational Wallpapers Pack 1
  *
  * Wallpaper Designed by AZ2ENVY
  *
  */

package com.death2all110.SensationalWallpapers1;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.widget.Button;
import android.content.DialogInterface;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; 


import com.death2all110.SensationalWallpapers1.R;


public class wallpaper extends Activity implements AdapterView.OnItemSelectedListener,
        OnClickListener {



    private Gallery mGallery;
    private ImageView mImageView;
    private boolean mIsWallpaperSet;

    private Bitmap mBitmap;

    private ArrayList<Integer> mThumbs;
    private ArrayList<Integer> mImages;
    private WallpaperLoader mLoader;



    @Override 
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);


        requestWindowFeature(Window.FEATURE_NO_TITLE);


        findWallpapers();

        setContentView(R.layout.wallpaper_chooser);

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemSelectedListener(this);
        mGallery.setCallbackDuringFling(false);

        findViewById(R.id.set).setOnClickListener(this);

        mImageView = (ImageView) findViewById(R.id.wallpaper);

        Button alert = (Button) findViewById(R.id.about_page);
        alert.setOnClickListener(this);

    }

    private void findWallpapers() {
        mThumbs = new ArrayList<Integer>(24);
        mImages = new ArrayList<Integer>(24);

        final Resources resources = getResources();
        final String packageName = getApplication().getPackageName();

        addWallpapers(resources, packageName, R.array.wallpapers);
        addWallpapers(resources, packageName, R.array.extra_wallpapers);
    }

    private void addWallpapers(Resources resources, String packageName, int list) {
        final String[] extras = resources.getStringArray(list);
        for (String extra : extras) {
            int res = resources.getIdentifier(extra, "drawable", packageName);
            if (res != 0) {
                final int thumbRes = resources.getIdentifier(extra + "_small",
                        "drawable", packageName);

                if (thumbRes != 0) {
                    mThumbs.add(thumbRes);
                    mImages.add(res);
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIsWallpaperSet = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
            mLoader.cancel(true);
            mLoader = null;
        }
    }

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
            mLoader.cancel();
        }
        mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
    }

    /*
     * When using touch if you tap an image it triggers both the onItemClick and
     * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
     * set the wallpaper once.
     */
    private void selectWallpaper(int position) {
        if (mIsWallpaperSet) {
            return;
        }

        mIsWallpaperSet = true;
        try {
            InputStream stream = getResources().openRawResource(mImages.get(position));
            setWallpaper(stream);
            setResult(RESULT_OK);
            finish();
        } catch (IOException e) {
            Log.e("Paperless System", "Failed to set wallpaper: " + e);
        }
    }

    public void onNothingSelected(AdapterView parent) {
    }

    private class ImageAdapter extends BaseAdapter {
        private LayoutInflater mLayoutInflater;

        ImageAdapter(wallpaper context) {
            mLayoutInflater = context.getLayoutInflater();
        }

        public int getCount() {
            return mThumbs.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView image;

            if (convertView == null) {
                image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
            } else {
                image = (ImageView) convertView;
            }

            int thumbRes = mThumbs.get(position);
            image.setImageResource(thumbRes);
            Drawable thumbDrawable = image.getDrawable();
            if (thumbDrawable != null) {
                thumbDrawable.setDither(true);
            } else {
                Log.e("Paperless System", String.format(
                    "Error decoding thumbnail resId=%d for wallpaper #%d",
                    thumbRes, position));
            }
            return image;
        }
    }

    public void onClick(View v) {
        selectWallpaper(mGallery.getSelectedItemPosition());

        }   
    public void onClick1(View about) {
        // If "About was clicked.....
        if (about == findViewById(R.id.about_page)) {
            // Prepare the alert box!!
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

            // Set message to display...
            alertbox.setMessage("Test.....");

            // Add a neutral button to the alert box AND assign a listener for said button...
            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener(){

                // click listener for box
                public void onClick(DialogInterface arg0, int arg1){
                    // Button was clicked!!
                    Toast.makeText(getApplicationContext(), "Dialog closed successfully!", Toast.LENGTH_LONG).show();
                }
            });
            // show it!!!
            alertbox.show();
    }
    }


    class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
        BitmapFactory.Options mOptions;

        WallpaperLoader() {
            mOptions = new BitmapFactory.Options();
            mOptions.inDither = false;
            mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;            
        }

        protected Bitmap doInBackground(Integer... params) {
            if (isCancelled()) return null;
            try {
                return BitmapFactory.decodeResource(getResources(),
                        mImages.get(params[0]), mOptions);
            } catch (OutOfMemoryError e) {
                return null;
            }            
        }

        @Override
        protected void onPostExecute(Bitmap b) {
            if (b == null) return;

            if (!isCancelled() && !mOptions.mCancel) {
                // Help the GC
                if (mBitmap != null) {
                    mBitmap.recycle();
                }

                final ImageView view = mImageView;
                view.setImageBitmap(b);

                mBitmap = b;

                final Drawable drawable = view.getDrawable();
                drawable.setFilterBitmap(true);
                drawable.setDither(true);

                view.postInvalidate();

                mLoader = null;
            } else {
               b.recycle(); 
            }
        }

        void cancel() {
            mOptions.requestCancelDecode();
            super.cancel(true);
        }
    }
}

Here is the logcat:

I/ActivityManager(   59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.death2all110.SensationalWallpapers1/.wallpaper }

I/ActivityManager(   59): Displayed activity com.death2all110.SensationalWallpapers1/.wallpaper: 474 ms (total 474 ms)

D/dalvikvm(  286): GC freed 1448 objects / 151304 bytes in 131ms

D/dalvikvm(  106): GC freed 2485 objects / 144824 bytes in 245ms

D/dalvikvm(   59): threadid=15: bogus mon 1+0>0; adjusting

D/dalvikvm(   59): GC freed 3666 objects / 207344 bytes in 360ms

D/dalvikvm(   59): GC freed 459 objects / 21096 bytes in 357ms

E/WindowManager(  286): Activity com.death2all110.SensationalWallpapers1.wallpaper has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c0f3d0 that was originally added here

E/WindowManager(  286): android.view.WindowLeaked: Activity com.death2all110.SensationalWallpapers1.wallpaper has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c0f3d0 that was originally added here

E/WindowManager(  286):     at android.view.ViewRoot.<init>(ViewRoot.java:227)

E/WindowManager(  286):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)

E/WindowManager(  286):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

E/WindowManager(  286):     at android.view.Window$LocalWindowManager.addView(Window.java:424)

E/WindowManager(  286):     at android.app.Dialog.show(Dialog.java:239)

E/WindowManager(  286):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)

E/WindowManager(  286):     at com.death2all110.SensationalWallpapers1.wallpaper.onClick(wallpaper.java:244)

E/WindowManager(  286):     at android.view.View.performClick(View.java:2364)

E/WindowManager(  286):     at android.view.View.onTouchEvent(View.java:4179)

E/WindowManager(  286):     at android.widget.TextView.onTouchEvent(TextView.java:6541)

E/WindowManager(  286):     at android.view.View.dispatchTouchEvent(View.java:3709)

E/WindowManager(  286):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

E/WindowManager(  286):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

E/WindowManager(  286):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)

E/WindowManager(  286):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)

E/WindowManager(  286):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

E/WindowManager(  286):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)

E/WindowManager(  286):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)

E/WindowManager(  286):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)

E/WindowManager(  286):     at android.os.Handler.dispatchMessage(Handler.java:99)

E/WindowManager(  286):     at android.os.Looper.loop(Looper.java:123)

E/Win开发者_StackOverflow社区dowManager(  286):     at android.app.ActivityThread.main(ActivityThread.java:4363)

E/WindowManager(  286):     at java.lang.reflect.Method.invokeNative(Native Method)

E/WindowManager(  286):     at java.lang.reflect.Method.invoke(Method.java:521)

E/WindowManager(  286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

E/WindowManager(  286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

E/WindowManager(  286):     at dalvik.system.NativeStart.main(Native Method)

D/dalvikvm(  286): GC freed 1704 objects / 168544 bytes in 330ms

Not only does it have a leaked window, but when that button is clicked it also sets the wallpaper...

Any help would be greatly appreciated


change this

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

with

AlertDialog.Builder alertbox = new AlertDialog.Builder(yourActivity.this);

EDITED:

Button alert = (Button) findViewById(R.id.about_page);

alert.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
          // do ur logic
          });

make above change to your button click


The error shows that you are using the bad context here,

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

Try to use the right context here,

getApplicationContext(); or getParent() may solve your issue...

Thanks, Suri Sahani.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜