0

I'm trying to create a custom dialog using onCreateContextMenu and onContextItemSelected. so when a user select the context menu it creates a dialog.

I'm using this code when a user select the first element:

if(item.getItemId() == 0) {

        try {

            imageUrl = new URL(UrlSprite);
            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
            conn.connect();
            loadedImage = BitmapFactory.decodeStream(conn.getInputStream());

            final Dialog d = new Dialog(ctx);
            d.requestWindowFeature(Window.FEATURE_NO_TITLE);
            d.setCancelable(true);

            d.setContentView(R.layout.popupsprite);

            TextView titulo = (TextView)d.findViewById(R.id.pkmnNombre);
            titulo.setText(Constantes.Pokemon[numero-1]);

            ImageView image = (ImageView)d.findViewById(R.id.spritePopup);
            image.setImageBitmap(loadedImage);

            d.show();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

But when I click the application closes. It only happens in Android 3.0+, in earlier versions, like 2.1, 2.2, 2.3, the popup shows correctly.

Any ideas?

Thanks.

Solution

final Dialog d = new Dialog(ctx);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCancelable(true);

d.setContentView(R.layout.popupsprite);

TextView titulo = (TextView)d.findViewById(R.id.pkmnNombre);
titulo.setText(Constantes.Pokemon[numero-1]);

ImagenSprite = (ImageView)d.findViewById(R.id.spritePopup);

attachImage(UrlSprite, ImagenSprite);

d.show();

And

public void attachImage(final String fileUrl, final ImageView view) {
    EXECUTOR.execute(new Runnable() {

        @Override
        public void run() {
            final Bitmap image = downloadImg(fileUrl);

            if (image != null) {
                view.post(new Runnable() {

                    @Override
                    public void run() {
                        view.setImageBitmap(image);
                    }
                });
            }
        }

        Bitmap downloadImg(String imgUrl) {

            try {
                URL imageUrl = new URL(imgUrl);
                HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
                conn.connect();
                loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return loadedImage;

        }

    });
}
4

2 回答 2

2

您正在 ui 线程上运行网络相关操作

HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.connect();

您应该使用ThreadAsyncTask。你会得到NetworkOnMainThreadException后蜂窝

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

所以创建自己的线程并在那里进行网络相关操作或使用 Asynctask。

http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-09-23T20:10:43.957 回答
0

除了使用RaghunandanDialogFragment之外,我认为它已被 Android 4.0+ 弃用(作为旁注)。

Android DialogFragment vs Dialog

于 2013-09-23T20:09:09.933 回答