onCreateContextMenu
と を使用してカスタム ダイアログを作成しようとしていますonContextItemSelected
。そのため、ユーザーがコンテキスト メニューを選択すると、ダイアログが作成されます。
ユーザーが最初の要素を選択するときに、このコードを使用しています。
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();
}
}
しかし、クリックするとアプリケーションが閉じます。Android 3.0+ でのみ発生し、2.1、2.2、2.3 などの以前のバージョンでは、ポップアップが正しく表示されます。
何か案は?
ありがとう。
解決
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();
と
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;
}
});
}