0

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 ?

Tom
  • 1,144
  • 1
  • 11
  • 23

2 Answers2

0

Perhaps you should replace getBaseContext() with your context. See top answer in this question

Community
  • 1
  • 1
f20k
  • 3,106
  • 3
  • 23
  • 32
  • That's what I've tried. The bindView method take a Context context in argument. I've tried to use it without success too... – Tom Jan 20 '11 at 19:01
  • Try using the debugger, deleting /drawable and also post the error message you got. Also try close/re-open emulator. Also, where is the bindView method in your code? I have not seen it. And what do you mean by 'removing it' ? Remove what? – f20k Jan 20 '11 at 20:01
  • This is not emulator related since it occurred first with my mdpi phone. The bindView is in a class extending CursorAdapter, and gets the context of the parent activity as an argument. By removing it, I mean calling the getRessource with nothing in front (removing the getBaseContext.) – Tom Jan 20 '11 at 22:01
  • ERROR/AndroidRuntime(332): FATAL EXCEPTION: main ERROR/AndroidRuntime(332): java.lang.NullPointerException at com.tom.test.TestActivity$AdapterTuning.bindView(TestActivity.java:377) at android.widget.CursorAdapter.getView(CursorAdapter.java:186) at android.widget.AbsListView.obtainView(AbsListView.java:1315) at android.widget.ListView.measureHeightOfChildren(ListView.java:1198) at android.widget.ListView.onMeasure(ListView.java:1109) at android.view.View.measure(View.java:8171)... The line 377 is the line of my question... – Tom Jan 20 '11 at 22:02
  • can you use the debugger to set a breakpoint at that bindview line? Then run your program on debug mode. When it hits that line, check which of the three parameters is null: view, context, or cursor. – f20k Jan 20 '11 at 22:24
  • Ok, It's solved... but I've learned how to use the debugger! It's a shame! The null variable was the ImageView, because I was too stupid to recall that I've got three layouts xml's too, one for each resolution, and I've only edited the hdpi one... Thanks a lot f20k! – Tom Jan 21 '11 at 00:10
0

What is your target SDK? It should be > 3 (>1.5)

Tom
  • 2,242
  • 18
  • 27
  • Of course it is >3 (it is 4!), and other (but not in a ListView) drawables are well fetched. – Tom Jan 20 '11 at 21:29