Get Drawable from image view

17,339

Solution 1

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.

Solution 2

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
        }
    }

Solution 3

None of the answers till now is correct

The issue here is that in the image view the drawable is attached using android:background="@drawable/clip" and it is retrieved using ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();

Fixes:

Change the imageview xml

android:background="@drawable/clip" -> android:src="@drawable/clip"

OR

Change the button callback code

ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); -> ClipDrawable drawable = (ClipDrawable) imageview.getBackground();

Don't change both, otherwise the problem will get created again. Change any one.

Solution 4

This is an easy approach if you can use the view's id member variable: just store the R.drawable id using v.setId(). Then get it back with v.getId().

Share:
17,339

Related videos on Youtube

Dinesh T A
Author by

Dinesh T A

Android app developer @Zoho Corporation

Updated on September 16, 2022

Comments

  • Dinesh T A
    Dinesh T A over 1 year

    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
    
  • Dinesh T A
    Dinesh T A about 11 years
    developer.android.com/guide/topics/resources/… look this link for clip drawable example i do the same in image i didn't give the src instead of i gave the background clip drawable
  • Parag Chauhan
    Parag Chauhan about 11 years
    android:background="@drawable/clip" have you set this?
  • She Smile GM
    She Smile GM about 11 years
    I updated my code above, instead of imageview.getDrawable changed to image.getBackground().
  • Dinesh T A
    Dinesh T A about 11 years
    thanks @SheSmile It's works fine i have add a button in button onclick i have do the drawable.setLevel