doInBackground執行報錯
完全按照視頻在AS下寫的,但一運行就報錯,提示java.lang.RuntimeException: An error occured while executing doInBackground(),關鍵就是is = connection.getInputStream()這句不知有什么問題。
protected Bitmap doInBackground(String... strings) {
? ?//獲取傳遞進來的參數
? ?String url = strings[0]; //取出對應url
? ?Bitmap bitmap = null;
? ?URLConnection connection; //定義網絡連接對象
? ?InputStream is; //用于獲取數據的輸入流
? ?try {
? ? ? ?connection = new URL(url).openConnection(); //獲取網絡連接對象
? ? ? ?is = connection.getInputStream();
? ? ? ?BufferedInputStream bis = new BufferedInputStream(is);
? ? ? ?bitmap = BitmapFactory.decodeStream(bis); //將輸入流解析成bitmap
? ? ? ?is.close();
? ? ? ?bis.close();
? ?} catch (IOException e) {
? ? ? ?e.printStackTrace();
? ?}
? ?return bitmap;
}
2015-08-16
我找到問題了,是權限添加的問題:不知道為何AS自動補全時,全部變成了大寫,我當時也沒注意。
<uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
應該是
<uses-permission android:name="android.permission.INTERNET"/>
這真的很奇怪,大寫也沒有報錯,而且是AS自動補全的,我當時只打了internet。