private void loadImg(){
AsyncTask a =new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
//text.setText("asdfasdfasdf");錯誤位置 出現NullPoint錯誤
return null;}
@Override
protected void onPostExecute(Void v){
text.setText("asdfasdfasdf");//正確位置}
}.execute();}
上面錯誤的原因在於: 我直接在doInBackground()中更新UI了
- doInBackground(Params…) 後臺執行,比較耗時的操作都可以放在這裏。注意這裏不能直接操作UI。此方法在後臺線程執行,完成任務的主要工作,通常需要較長的時間。在執行過程中可以調用publicProgress(Progress…)來更新任務的進度。
- onPostExecute(Result) 相當於Handler 處理UI的方式,在這裏面可以使用在doInBackground 得到的結果處理操作UI。 此方法在主線程執行,任務執行的結果作爲此方法的參數返回
BitmapFactory.Options的使用 inSampleSize
@Override
protected void onPostExecute(String v){
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=4; //縮小爲16倍 寬1/4 高1/4
Bitmap bitmap=BitmapFactory.decodeFile(v,options);
img.setImageBitmap(bitmap);
}