내가이 클래스 MyView
을 말해봐 인레이가 setContentView
인 경우 init
이 MyView
으로 호출되기 전에 mRenderer
이 아직 초기화되지 않았습니다.뷰의 속성에 따라 물건을 초기화하는 방법
속성이 설정된 후에 발생하는보기의 초기화 없이는보기의 속성을 설정할 수 없으므로 catch 22처럼 보입니다. 당신이 그것을하고있는 같다
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<com.developer.app.MyView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
/>
<TextView
android:id="@+id/left_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/left_arrow"
/>
<TextView
android:id="@+id/right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/right_arrow"
/>
<TextView
android:id="@+id/home_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/home"
/>
</RelativeLayout>
실제로이 코드를 실행 했습니까? 'MyView' 뷰가 레이아웃 XML'R.layout.my_layout'의 일부라고 가정합니다. 그렇다면'setContentView'는'MyView'의 생성자를 호출하게 만들 것입니다. 어쨌든 모든 장점으로'setBackground'는 인스턴스 메소드이며 렌더러가 초기화되기 전에 코드 흐름에서 호출 할 수있는 방법이 없습니다 (생성자에서 렌더러를 초기화하는 중입니다) – Dibzmania
@Dibzmania이 코드는 주석을 달고 있습니다. 'MyView'에서'setBackground'를 꺼내거나 수퍼 클래스의'setBackground' 메소드를 호출하는 것입니다. 이전의보기는 검정색 배경이됩니다. 이후에는 렌더링 클래스의 onDrawFrame 메소드에 모든 draw 호출이있을 때에도 배경이 수퍼 클래스에 의해 렌더링되지만 내 지오메트리는 렌더링되지 않습니다. – rraallvv
생성자 또는 init 메소드에서'setZOrderOnTop (true)'를 호출 해보십시오. 그리고 나서'setBackground' 메쏘드의 주석 처리를 제거하고 작동하는지 확인하십시오. – Dibzmania