我收到有关 Google Play 中某个应用的崩溃报告。我使用异步任务来获取图像,它适用于我测试的每台设备,但有些用户遇到了问题。这真的很少见,但我需要调试和修复它。这是 doInBackground 方法:
public class MetaTask extends AsyncTask<Void, Void, Void> {
URL imageurl;
@Override
protected Void doInBackground(Void... params) {
try {
doc = Jsoup.connect(data).get();
Elements meta = doc.select("meta[property=og:image]");
for (Element element : meta) {
extracted = element.attr("content");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
imageurl = new URL(extracted);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
空指针似乎在这一行(271):
bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
.getInputStream());
由于某种原因,我正在考虑变量 imageurl 不是实际的 url,我唯一能想到的是检查提取的包含“http”和“jpg”的字符串变量,如果这些变量不存在,则使用 Flurry 存储此信息.
在执行 asynctask 之前,会检查 Internet 连接的可用性,因此不会导致这种情况。
谁能想到导致这种情况的其他原因?这是我对崩溃报告的完整堆栈跟踪记录:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.ddroid.photosaveinstagram.MainActivity$MetaTask.doInBackground(MainActivity.java:271)
at com.ddroid.photosaveinstagram.MainActivity$MetaTask.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more