AndroidDev 板


LINE

public class MorePhotoDialog extends DialogFragment { private static final String TAG = "MorePhotoDialog"; private CustomAdapter _customAdapter; private Context _context; private ListView _listView; private LinearLayout _linearLayout; private ProgressBar _progressBar; public MorePhotoDialog(Context context) { Log.d(TAG, "new MorePhotoDialog"); _customAdapter = new CustomAdapter(context); _context = context; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.custom_adapter, null); _progressBar = (ProgressBar) view .findViewById(R.id.custom_adapter_progressBar2); _linearLayout = (LinearLayout) view .findViewById(R.id.custom_adapter_progressLine); _listView = (ListView) view.findViewById(R.id.custom_adapter_listView); _listView.setAdapter(_customAdapter); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); builder.setTitle(R.string.more_photo_dialog_more); return builder.create(); } int endFlag = 0; public void addImageLoaderTask(String... urls) { endFlag = urls.length - 1; for (int i = 0; i < urls.length - 1; i++) { ImageLoaderTask imageLoaderTask = new ImageLoaderTask(i); Log.d(TAG, "imageLoaderTask.execute: " + urls[i]); imageLoaderTask.execute(urls[i]); } } // private class ImageLoaderTask extends AsyncTask<String, Void, Bitmap[]> { private class ImageLoaderTask extends AsyncTask<String, Void, Void> { private int _flag = 0; private ProgressBar progressBar; public ImageLoaderTask(int flag) { _flag = flag; // progressBar = (ProgressBar) getActivity().findViewById(R.id.custom_adapter_progressBar2); } @Override protected void onPreExecute() { // TODO progress bar // Display progress bar if (_flag == 0) { _linearLayout.setVisibility(View.VISIBLE); // progressBar.setVisibility(View.VISIBLE); } } @Override protected Void doInBackground(String... url) { String imgUrl = url[0]; Log.w(TAG, "url[0] = " + url[0]); // Getting Caching directory File cacheDirectory = _context.getCacheDir(); // Temporary file to store the downloaded image File tmpFile = new File(cacheDirectory.getPath(), FunctionUtility.getLastString(imgUrl)); Log.d(TAG, "imgUrl(" + tmpFile.getName() + ") = " + imgUrl); if (!tmpFile.exists()) { Log.w(TAG, tmpFile.getPath() + " not exists"); BitmapFuntions.downloadBitmapFromUrl(imgUrl, tmpFile); } // Load bitmap and put in adapter Bitmap bitmap = BitmapFuntions.decodeFile(tmpFile, 300, 200); _customAdapter.addBitmap(bitmap); // Show Logs Log.d(TAG, "bitmap.getByteCount() = " + bitmap.getByteCount()); Log.d(TAG, tmpFile.getPath()); Log.d(TAG, tmpFile.getName() + " = " + tmpFile.length() / 1024 + " Kb"); return null; } @Override // protected void onPostExecute(Bitmap[] result) { protected void onPostExecute(Void result) { Log.d(TAG, "onPostExecute"); _customAdapter.notifyDataSetChanged(); if (_flag == endFlag) { _linearLayout.setVisibility(View.GONE); //progressBar.setVisibility(View.GONE); } } } } 呼叫dialogFragment的方法是 // Show MorePhotoDialog MorePhotoDialog morePhotoDialog = new MorePhotoDialog( Image_enlarge.this); morePhotoDialog .show(getSupportFragmentManager(), "morePhotoDialog"); morePhotoDialog.addImageLoaderTask(imglist); 可是会跑出NullPointerException 07-24 14:59:22.000: E/AndroidRuntime(7064): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference 07-24 14:59:22.000: E/AndroidRuntime(7064): at com.gundam.cool.dialog.MorePhotoDialog$ImageLoaderTask.onPreExecute(MorePhotoDialog.java:96) 07-24 14:59:22.000: E/AndroidRuntime(7064): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 07-24 14:59:22.000: E/AndroidRuntime(7064): at android.os.AsyncTask.execute(AsyncTask.java:535) 不懂为什麽抓不到view... --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.36.155.148
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1406228461.A.A3C.html
1F:推 markzog21:你把要显示的view丢到asyncTask里面去试试看 07/25 16:53
2F:→ markzog21:要显示dialog的view 07/25 16:53
试过了,也是一样的结果
3F:→ corrupt003:你的activity的layout里有view叫 07/25 20:07
4F:→ corrupt003:R.id.custom_adapter_progressBar2 吗? 07/25 20:07
有,我是用alt+/找到的 ※ 编辑: comteken (140.124.249.8), 07/26/2014 09:25:59
5F:推 corrupt003:alt + / 是找到R.java的所有资料…你还是要看activity 07/26 09:30
6F:→ corrupt003: 的layout 有没有订义这个id,因为你是用getActivity去 07/26 09:30
7F:→ corrupt003:取得view的 07/26 09:30
8F:推 freesamael:morePhotoDialog.show() 是 non-blocking call, 07/29 19:48
9F:→ freesamael:FragmentManager 会排程到下一次 main thread 空闲时 07/29 19:48
10F:→ freesamael:才把 DialogFragment 加到 activity。因此 07/29 19:49
11F:→ freesamael:onCreateDialog() 被呼叫的时间会在 07/29 19:49
12F:→ freesamael:addImageLoaderTask() 之後,此时 layout 还没设定 07/29 19:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP