I have a logo image that I display on the splash/welcome screen of the my app. When I recently made the app compliant to the xxx-hdpi screen sizes , I encounter a weird problem:
- I created a bigger logo image with "_large" added to the name. I put that and the other image in
drawable
folder. - In the
onCreate
function of the activity, I'm checking device's density and opening the appropriate image:
float MAX_XX_SCALE = 3.0F; //Maximum scale of xx-hdpi screens.
ImageView img = (ImageView) findViewById(R.id.myImg); // ImageView containing the image.
if (getResources().getDisplayMetrics().density > MAX_XX_SCALE)
img.setImageDrawable(getResources().getDrawable(R.drawable.logo_large));
else
img.setImageDrawable(getResources().getDrawable(R.drawable.logo));`
But I'm getting a NullPointerException
while executing the app.
This is my logcat
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.os.Looper.loop(Looper.java:137)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-11 11:53:46.625: E/AndroidRuntime(919): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:53:46.625: E/AndroidRuntime(919): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 11:53:46.625: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-11 11:53:46.625: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-11 11:53:46.625: E/AndroidRuntime(919): at dalvik.system.NativeStart.main(Native Method)
11-11 11:53:46.625: E/AndroidRuntime(919): Caused by: java.lang.NullPointerException
11-11 11:53:46.625: E/AndroidRuntime(919): at com.project.android.WelcomeActivity.onCreate(WelcomeActivity.java:42)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.Activity.performCreate(Activity.java:4465)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-11 11:53:46.625: E/AndroidRuntime(919): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
This is what I have on line 42: img.setImageDrawable(getResources().getDrawable(R.drawable.logo));
setContentView()