mrwonderman / android-square-progressbar

An android library to display a progressbar that goes around an image.
http://halcyon.ch/android-square-progressbar-v-1-6-0/
1.28k stars 285 forks source link

NPE on the setImageScaleType(ImageView.ScaleType.CENTER); #27

Closed gordonpro closed 9 years ago

gordonpro commented 9 years ago

Why the image reference assigning after constructor called. It may cause NPE if someone calls setImageScaleType before setting an image. squareprogressbar 93 line


    public void setImage(int image) {
        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageResource(image);

    }
mrwonderman commented 9 years ago

Hi @gordonpro

You're right if you call the setImageScaleType before setting an image you're getting an NPE. See here:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ch.halcyon.squareprogressbar.example/ch.halcyon.squareprogressbar.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setScaleType(android.widget.ImageView$ScaleType)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setScaleType(android.widget.ImageView$ScaleType)' on a null object reference
            at ch.halcyon.squareprogressbar.SquareProgressBar.setImageScaleType(SquareProgressBar.java:108)
            at ch.halcyon.squareprogressbar.example.SquareFragment.onCreateView(SquareFragment.java:22)
            at android.app.Fragment.performCreateView(Fragment.java:2114)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
            at android.app.BackStackRecord.run(BackStackRecord.java:833)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.app.Activity.performStart(Activity.java:6257)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I will move it to the constructor and then it works fine. Thanks for bringing that to my attention.

Regards Yannick

mrwonderman commented 9 years ago

The changes are in the develop-branch now. This bugfix will be included in the next version: 1.5.2. I will close this issue as soon as that version is published.

mrwonderman commented 9 years ago

available in version 1.5.2