3

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

日志猫

 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

4 回答 4

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() 将通知用户视图已经加载,此时您可以获取其数据。

于 2013-02-04T08:47:24.150 回答
1

可能你会得到null drawable所以这个错误会发生。

首先set 任何image到,ImageView所以你会得到可绘制的。

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
        }
    }
于 2013-02-04T07:09:15.830 回答
1

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

于 2014-05-19T03:56:32.063 回答
1

到目前为止,没有一个答案是正确的

这里的问题是,在图像视图中,可绘制对象是使用附加的android:background="@drawable/clip",并且是使用检索的ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();

修复:

更改图像视图 xml

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

或者

更改按钮回调代码

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

不要同时更改两者,否则问题将再次出现。改变任何一个。

于 2014-12-22T09:24:32.097 回答