helloWorld XML 레이아웃에서 사용자 정의 뷰를 참조하려고하는데 다음 예외가 발생합니다. acme.my.MyTextView 클래스를 확장하는 중 오류가 발생했습니다.레이아웃에서 사용자 정의 뷰를 참조하는 중 문제가 발생했습니다.
그러나보기를 인스턴스화하고 기본 콘텐츠보기에 수동으로 추가 할 수 있습니다. 커스텀 뷰는 자신의 XML 레이아웃을 기반으로 구축됩니다. 어떻게 작동 시키나요?
public class MyTextView extends LinearLayout {
MyTextView(Context context){
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
MyTextView(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<view class = "acme.my.MyTextView"
android:id="@+id/myView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
@fredrick 확인이
과 같이 호출 할 필요가 : 일반적으로하는이 설명이 인용 한 아래의 예외 '에 의한'있다 무엇이 잘못되었는지에 대해 좀 더 자세히 알아보기 – CommonsWare
MyTextView (Context, AttributeSet) 생성자 맨 위에 중단 점이 있습니다. 결코 깨지지 않습니다. – Fredrick
아주 간단한 레이아웃으로이 작업을 수행 할 수 있습니다. 보다 복잡한 레이아웃에서 무엇을 깨뜨리고 있는지 파악하십시오. – Fredrick