2012-05-21 10 views
0

사용자 지정 대화 상자가 있습니다. "확인"을 클릭하면 EditText 필드에서 텍스트를 가져오고 싶습니다. 그러나 null 포인터 예외가 발생합니다.OnClickListener의 EditText에서 getText를 호출 할 때 Null 포인터 예외가 발생합니다.

코드 :

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.dialog_delivered, (ViewGroup)findViewById(R.id.llDeliveredDialog)); 

builder.setMessage("Dialog message"); 
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
    final EditText text = (EditText)findViewById(R.id.etGivenTo); 
    String value = text.getText().toString(); 
    } 
}); 

builder.setCancelable(false); 
builder.setView(layout); 
builder.show(); 

XML 레이아웃 : 어떤 도움이 많이 주시면 감사하겠습니다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/llDeliveredDialog" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/etGivenTo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

</LinearLayout> 

.

+0

에서 onCreate는 방법이 입력 : 최종 글고 텍스트 = (글고) findViewById를 (R.id.etGivenTo); 다시 시도하십시오. –

답변

7
final EditText text = (EditText)layout.findViewById(R.id.etGivenTo); 
           ^^^^^^ 
+0

레이아웃을 final로 설정하면 완벽하게 작동합니다. 감사 – aggregate1166877

1
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
    final EditText text = (EditText)layout.findViewById(R.id.etGivenTo); 
    String value = text.getText().toString(); 
    } 
}); 
관련 문제