单击对话框按钮时泄露的窗口

时间:2011-09-15 04:40:12

标签: java android onclick alertdialog

当我们点击“关于”按钮时,我正试图向我的壁纸应用程序的用户显示一个关于页面但是我在日志中发现了泄漏的窗口错误,并且活动在显示对话框之前退出。

以下是代码:

 /*
  *
  * 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);
        }
    }
}

这是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/WindowManager(  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

它不仅有泄漏的窗口,而且当点击该按钮时它也会设置壁纸......

非常感谢任何帮助

2 个答案:

答案 0 :(得分:3)

更改此

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

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
          });

将上面的更改改为按钮

答案 1 :(得分:1)

错误显示您在此处使用了错误的上下文

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

尝试在此处使用正确的上下文

getApplicationContext(); or getParent()可以解决您的问题...

谢谢, Suri Sahani。