저는 Activity 내에서가 아니라 Java 클래스 내에서 다음을 수행합니다.OnClickListener를 버튼에 추가하려고 할 때 NullPointerException이 발생했습니다.
LinearLayout buttonLayout = (LinearLayout) parentContext.getLayoutInflater().inflate(R.layout.app_dialog, null);
Button btnSignIn = (Button) parentContext.findViewById(R.id.BTN_ID_SIGN_IN);
Button btnCancel = (Button) parentContext.findViewById(R.id.BTN_ID_CANCEL);
btnSignIn.setOnClickListener(new OnClickListener(){
public void onClick(View v) { ... }});
레이아웃 XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_alignParentBottom="true" style="@android:style/ButtonBar" >
<Button android:id="@+id/BTN_ID_SIGN_IN" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="@string/em_btn_signin" />
<Button android:id="@+id/BTN_ID_CANCEL" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="@string/em_btn_cancel" />
</LinearLayout>
이 마지막 줄은 NPE를 던지고있다. onClickListener없이 코드를 시도하면 버튼이 완벽하게 표시됩니다 (obv는 탭하면 아무 일도 발생하지 않음). 왜 내가 버튼에 아무것도 넣지 않을거야?
더 많은 코드를 게시 할 수 있습니까? parentContext는 무엇입니까? –
parentContext는 호출하는 Activity의 컨텍스트입니다. 나는 활동이 아닌 별도의 수업에서 이것을하고있다.) – PlanetaryFortressRush
왜 팽창의 두 번째 매개 변수로 "null"이 있습니까? 두 번째 매개 변수는 루트 레이아웃이어야합니다. – 0gravity