从图像视图中获取Drawable

时间:2013-02-04 06:43:35

标签: android android-layout android-drawable

当我执行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

4 个答案:

答案 0 :(得分:2)

您必须覆盖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()将通知用户已经加载了视图,这是您获取数据的时间。

答案 1 :(得分:1)

您可能会null drawable,因此会出现此错误。

首先set任意imageImageView,这样您就可以获得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
        }
    }

答案 2 :(得分:1)

到目前为止,答案都没有正确

此处的问题是,在图片视图中,使用android:background="@drawable/clip"附加了drawable,并使用ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();

进行检索

修正:

更改imageview xml

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

更改按钮回调代码

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

不要同时更改两者,否则会再次创建问题。改变任何一个。

答案 3 :(得分:0)

如果您可以使用视图的id成员变量,这是一种简单的方法:只需使用v.setId()存储R.drawable id。然后使用v.getId()将其恢复。