【问题标题】:Android - imageView.getDrawable() returns nullAndroid - imageView.getDrawable() 返回 null
【发布时间】:2015-09-25 01:04:33
【问题描述】:

我被这个看似简单的问题困住了。我做错了什么?

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);

    }

应用程序在 drawable.setLevel(10) 处崩溃并出现 NullPointerException(drawable 为 null)。

这是我的 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>

剪辑.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

<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>

请帮帮我!

【问题讨论】:

    标签: android android-imageview android-drawable


    【解决方案1】:

    你将drawable设置为'src'的background属性。

    android:background="@drawable/clip"

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

    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()来获取你的drawable,该方法返回由属性android:background设置的drawable

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2012-05-28
      • 2014-12-05
      • 2021-07-07
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 2012-06-28
      相关资源
      最近更新 更多