When I do the clip drawable example described in this document ImageView.getDrawable always return null. Can anyone pls help me?
In MainActivity.java onCreate
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 1000); //Line number 21
}
Logcat
02-04 12:16:31.156: E/AndroidRuntime(4611): FATAL EXCEPTION: main02-04 12:16:31.156: E/AndroidRuntime(4611): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.os.Looper.loop(Looper.java:123)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-04 12:16:31.156: E/AndroidRuntime(4611): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:16:31.156: E/AndroidRuntime(4611): at java.lang.reflect.Method.invoke(Method.java:507)
02-04 12:16:31.156: E/AndroidRuntime(4611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 12:16:31.156: E/AndroidRuntime(4611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 12:16:31.156: E/AndroidRuntime(4611): at dalvik.system.NativeStart.main(Native Method)
02-04 12:16:31.156: E/AndroidRuntime(4611): Caused by: java.lang.NullPointerException
02-04 12:16:31.156: E/AndroidRuntime(4611): at com.example.test.MainActivity.onCreate(MainActivity.java:21)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 12:16:31.156: E/AndroidRuntime(4611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-04 12:16:31.156: E/AndroidRuntime(4611): ... 11 more
You have to override onWindowsFocusChanged()
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
drawable.setLevel(drawable.getLevel() + 1000); //Line number 21
}
the imageView was not yet displayed properly when you tried to get its value(drawable image). onWindowsFocusChanged() will inform the user that the view has been loaded already that's the time you can get its data.
May be you will get null drawable so this error will occure.
First set any image to ImageView so you will get Drawable.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.image);
imageview.setBackgroundResource(R.drawable.icon);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
if(drawable != null){
drawable.setLevel(drawable.getLevel() + 1000); //Line number 21
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With