其他分享
首页 > 其他分享> > android-从图像视图获取可绘制

android-从图像视图获取可绘制

作者:互联网

当我执行this文档ImageView.getDrawable中描述的剪辑可绘制示例时,始终返回null.有人可以帮助我吗?

在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

解决方法:

您必须重写onWindowsFocusChanged()

 @Override
 public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
   ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
    drawable.setLevel(drawable.getLevel() + 1000); //Line number 21
 }

尝试获取其值(可绘制图像)时,imageView尚未正确显示.
onWindowsFocusChanged()将通知用户该视图已被加载,这是您可以获取其数据的时候了.

标签:android-layout,android-drawable,android
来源: https://codeday.me/bug/20191031/1974709.html