Android 的一些 mdpi 和 ldpi 资源出现问题
我有一个支持从 hdpi 到 ldpi 的应用程序。它工作正常,访问资源没有任何问题。 在此应用程序中,我有一个 ListView,我决定在列表的每个视图中添加一个图像。所以我准备了三张png图片,每种分辨率一张,并将它们放在各自的资源目录中。
使用 hdpi 2.2 模拟器一切正常,但在 2.2 mdpi 和 ldpi 中,在我将可绘制对象放入 iImageView 的 bindView 方法行处,它崩溃并出现 NullPointerException: mImageView.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.hello)
我尝试用 getApplicationContext()
替换 getBaseContext()
,甚至删除它,但没有成功。
我猜该应用程序不会在 -mdpi 文件夹中查找。但是,我不明白为什么 hdpi 版本工作正常。请注意,裸露的可绘制目录中没有任何内容。 此外,应用程序知道如何查看这些文件夹,因为它在用于启动带有 ListView 的文件夹的上一个活动中执行此操作。
有什么想法吗?
I have an app supporting from hdpi to ldpi. It works fine, without any problem for accessing resources.
In this app, I have a ListView, and I decided to add an image in each view of the list. So I prepared three png images, one for each resolution, and placed them in their respective resource directories.
All is fine with a hdpi 2.2 emulator, but it crashes with a NullPointerException in 2.2 mdpi and ldpi at the bindView method line where I put the drawable in the iImageView:mImageView.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.hello)
I've tried to replace getBaseContext()
by getApplicationContext()
or even removing it without success.
I guess that the app doesn't look in the -mdpi folder. However, I don't understand why the hdpi version works fine. Note that there is nothing in the bare drawable directory.
Furthermore, the app knows how to look in these folders since it does it in a previous activity used to launch the one with the ListView.
Any Idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该用您的上下文替换 getBaseContext() 。请参阅此问题中的最佳答案
Perhaps you should replace getBaseContext() with your context. See top answer in this question
您的目标 SDK 是什么?应该是> 3(>1.5)
What is your target SDK? It should be > 3 (>1.5)