Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-imageView.getDrawable()返回null_Android_Android Imageview_Android Drawable - Fatal编程技术网

Android-imageView.getDrawable()返回null

Android-imageView.getDrawable()返回null,android,android-imageview,android-drawable,Android,Android Imageview,Android Drawable,我被困在这个看似简单的问题上。我做错了什么 ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView)findViewB

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

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.setLevel10崩溃,出现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>
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

<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>
请帮帮我

您设置了'src'的drawable to background属性

android:background=@drawable/clip

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

android:src=@drawable/clip

最后,您的活动_main.xml将是:

或者,您可以通过调用getBackground方法来获取drawable,该方法返回由属性android:background设置的drawable

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