에 의해 레이아웃 객체를 가져올 수 없습니다 이것은 XML 코드 :안드로이드 : 나는 그것의 ID
View mainLayout = findViewById(R.id.mainLayout);
// Or: LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
Log.d("LAYOUT", mainLayout == null ? "NULL" : "NOT NULL"); // Always prints null
배치는 항상 null입니다 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
이
자바 코드입니다.왜 그럴까요? id는 정확하며 다른 모든 요소는 문제없이 가져올 수 있습니다.
보기를 팽창/생성해야합니다. findViewById()에 의해 접근 가능해야한다. 이보기가 표시되는지 (예 : 활동에 표시되는 경우) 또는이 컨텍스트 외부에서 표시해야하는 경우 – NuSkooler
잘 모르겠지만 당신의 프로젝트를 청결하게 만들어야한다고 생각합니다. –