Get the ability to draw from an image

When I do the clip drawing example described in this document ImageView.getDrawable always returns null. Can anyone 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

      

+3


source to share


4 answers


You need 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 correctly when you tried to get its value (the drawn image). onWindowsFocusChanged () will tell the user that the view is already loaded when you can get its data.

+2


source


You might get null drawable

this error to occur.

set

Any image

before first ImageView

so that you 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
        }
    }

      

+1


source


None of the answers are correct so far

The problem is that in the image view the drawable is attached with android:background="@drawable/clip"

and retrieved withClipDrawable drawable = (ClipDrawable) imageview.getDrawable();

Fixes:

Change the xml image

android:background="@drawable/clip"

android:src="@drawable/clip"

OR

Change the button callback code

ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();

ClipDrawable drawable = (ClipDrawable) imageview.getBackground();

Do not change both parameters, otherwise the problem will be created again. Change any.

+1


source


This is a simple approach if you can use a member variable of the kind id: just store the R.drawable id using v.setId (). Then return it with v.getId ().

0


source







All Articles