나는이 겉보기에 쉬운 문제에 갇혀있다. 내가 도대체 뭘 잘못하고있는 겁니까 ?
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>
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>
제발 도와주세요!
드로어 블을 background
'src'속성으로 설정했습니다 .
android : background = "@ drawable / clip"
. 따라서 속성을 다음으로 변경 activity_main.xml
하여 변경하십시오 .background
src
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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다