Android-imageView.getDrawable()返回null [英] Android - imageView.getDrawable() returns null

查看:893
本文介绍了Android-imageView.getDrawable()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个看似简单的问题上,我陷入了困境.我在做什么错了?

I am stuck on this seemingly easy issue. What am I doing wrong ?

ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView)findViewById(R.id.control);
        ClipDrawable drawable = (ClipDrawable)mImageView.getDrawable();
        drawable.setLevel(10);

    }

应用程序在NullPointerException(drawable为null)时在drawable.setLevel(10)崩溃.

The app crashes at drawable.setLevel(10) with a NullPointerException (drawable is null).

这是我的xml:

circle.xml

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:innerRadius="50dp"
    >

    <solid
        android:color="#FFAAFF"/>

</shape>

clip.xml

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@drawable/circle"
    android:gravity="clip_horizontal"

/>

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:background="@drawable/clip"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

请在这里帮助我!

推荐答案

您将drawable设置为'src'的background属性.

You set the drawable to background attribute of 'src'.

android:background ="@ drawable/clip"

android:background="@drawable/clip"

.因此,通过将background属性替换为src来更改您的activity_main.xml:

. So change yours activity_main.xml by replacing background attribute to src:

android:src ="@ drawable/clip"

android:src="@drawable/clip"

最后您的activity_main.xml将是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
    android:id="@+id/image"
    android:layout_height="100dp"
    android:layout_width="100dp"
    android:scr="@drawable/clip"
    android:layout_centerHorizontal="true"
    />

或者您可以通过调用方法getBackground()来获取可绘制对象,该方法将返回由属性android:background设置的可绘制对象

Or you can get yours drawable by calling method getBackground() which returns drawable that was set by attribute android:background

这篇关于Android-imageView.getDrawable()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆