나는이 주제가 새로운 것이 아니라는 것을 알고 있지만, 실제로 이것으로 붙어서 많은 답을 시도했지만 여전히 쓰고 사용해야하는 내용을 분명히 할 수는 없다.프로그래밍 방식으로보기를 올바르게 추가하는 방법은 무엇입니까?
나는 내가 프로그래밍 방식으로 추가 할 TextView passdel
이이 레이아웃 파일 Settings.XML의
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
<TextView
android:id="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/pass_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:text="Удалить пароль"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
</LinearLayout>
있습니다. 그 onCreate
설명 passdel=(TextView) findViewById(R.id.pass_del);
내가
public void onPasSet() {
pass.setText("Сменить пароль");
((LinearLayout)passdel.getParent()).addView(passdel);
}
public void onPasDel() {
pass.setText("Установить пароль");
((LinearLayout)passdel.getParent()).removeView(passdel);
}
onPasDel
View
을 제거하면이 레이아웃에 표시됩니다. 그리고이
View
을 추가하려고하면 Java가 현재 레이아웃에서이 뷰를 찾으려고 시도하지만 제거되었으므로 ..nullpointerexception.
addView
모든 내용을 올바르게 작성하려면 어떻게해야합니까? 필요한 레이아웃을 가리키는 방법은 무엇입니까?
다음에 전화를 걸면 어떻게됩니까? ((LinearLayout) passdel.getParent()). addView (passdel); 어딘가에'passdel'을 추가한다면 아마 부모가 없기 때문에'getParent'는 null 이외의 것을 반환 할 수 없습니다. 레이아웃을 직접 참조해야합니다. – njzk2