提问人:MohitC 提问时间:10/15/2019 更新时间:10/15/2019 访问量:83
NullPointerException 与 android AsyncTask 和 RecyclerView
NullPointerException with android AsyncTask and RecyclerView
问:
我有一个 AsyncTask 类可以从 Web 下载图像并将其设置为 in (从这里引用ImageView
)
相关活动代码:
public class ListAdoptions extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_adoptions);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new PetsAdapter(petList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
preparePetData();
}
private void preparePetData() {
DownloadImageTask imgtask = new DownloadImageTask((ImageView) findViewById(R.id.picture));
imgtask.execute("https://i.ytimg.com/vi/opKg3fyqWt4/hqdefault.jpg");
mAdapter.notifyDataSetChanged();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
错误:
Process: com.example.test3, PID: 8427
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.example.petadoption.ListAdoptions$DownloadImageTask.onPostExecute(ListAdoptions.java:171)
at com.example.petadoption.ListAdoptions$DownloadImageTask.onPostExecute(ListAdoptions.java:150)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答:
1赞
Adnan Ahmetgil
10/15/2019
#1
看不到整个代码,但消息是空对象... 确保您在 ListAdoptions 中定义了 bmImage,例如 findViewById(R.id.bmImage),并检查此对象是否存在于 id 为 bmImage 的 xml 中
评论
0赞
MohitC
10/15/2019
bmimage 是 asyncTask 的局部变量
1赞
Evan
10/15/2019
#2
处理 null 时要做的事情:
- 确保名称拼写正确
- 添加 if 语句以查看何时变为 null
- 使用调试器并单步执行代码,以查看对象为 null 的原因
由于某种原因,bmImage 不存在,我建议查看您的布局和此类本身以查找 null
评论
0赞
MohitC
10/15/2019
bmImage 是 asynctask 的局部变量
1赞
Alexandr Ukolov
10/15/2019
#3
看起来你的总是空的。很可能始终为 null。请确保使用 id “picture” 定义了 ImageView,并且没有在 XML 布局文件中遗漏 with plus。bmImage
findViewById(R.id.picture)
android:id="@+id/picture"
评论