2013-03-12 6 views
1

사용자 지정 alertdialog를 만들고 싶습니다. 다음 코드를 사용했습니다. 내가이setcontentview를 사용하는 Alertdialog

final MyAlertDialog alert = new MyAlertDialog(Home.this); 
alert.show(); 
Button ok=(Button)alert.findViewById(R.id.button1); 
Button cancel=(Button)alert.findViewById(R.id.button2); 

ok.setOnClickListener(new OnClickListener() 
{ 

    @Override 
    public void onClick(View v) 
    { 
     // TODO Auto-generated method stub 
    } 
}); 

처럼 버튼을 만든 사용자 지정을 사용하려고하면

public class MyAlertDialog extends AlertDialog { 

    protected MyAlertDialog(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void show() { 
     // TODO Auto-generated method stub 
     super.show(); 
     setContentView(R.layout.logindialog); 
    } 
} 

logindialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/layoutgrey" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:textSize="@dimen/mediuem" 
     android:text="username" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:textSize="@dimen/mediuem" 
     android:inputType="textPersonName" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="12dp" 
     android:textSize="@dimen/mediuem" 
     android:text= Password" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView2" 
     android:textSize="@dimen/mediuem" 
     android:inputType="textPersonName" /> 

    <Button 
     android:id="@+id/button2" 
     android:background="@drawable/buttons" 
     android:layout_width="100dp" 
     android:layout_height="40dp" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:text="Cancel" /> 

    <Button 
     android:id="@+id/button1" 
     android:background="@drawable/buttons" 
     android:layout_width="100dp" 
     android:layout_height="40dp" 
     android:layout_below="@+id/editText2" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="28dp" 
     android:layout_alignParentLeft="true" 
     android:text="Ok" /> 

</RelativeLayout> 

그래서 그것은 nullpointer 예외를 말한다. 여기서 문제가 무엇인지 알아 내주세요. 사전

답변

0

당신은 확실히 몇 가지 overengineering을하고있는에

감사합니다, 단지 AlertDialog'sBuilder에 사용자 정의 View으로 setView()를 호출합니다. 희망이 도움이됩니다.

1

다음은 사용자 정의 예 아니요 대화 상자입니다. 그것은 당신이 성취하고자하는 것과 비슷합니다. 레이아웃에 쉽게 적용 할 수 있습니다.

찾기 생성자에서 사용자 정의 레이아웃을 부풀려 사용자 설정의 아이디

에 의해 ButtonButtonOnClickListener : 내부 onClick() 전화 당신이 나중에 구현해야합니다 추상적 인 방법.

public abstract class YesNoDialog extends Dialog 
{ 
    private String mMessage = ""; 
    private TextView mText; 
    private Button mYes; 
    private Button mNo; 

    public YesNoDialog(Context context, String message) 
    { 
     super(context); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     View v = getLayoutInflater().inflate(R.layout.yes_no_dialog, null); 
     mText = (TextView) v.findViewById(R.id.message); 
     mMessage = message; 
     mText.setText(mMessage); 
     mYes = (Button) v.findViewById(R.id.yes); 
     mYes.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       dismiss(); 
       onYes(); 
      } 
     }); 
     mNo = (Button) v.findViewById(R.id.no); 
     mNo.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 
       dismiss(); 
       onNo(); 
      } 
     }); 
     setCancelable(false); 
     setCanceledOnTouchOutside(false); 
     setContentView(v); 
    } 

    public abstract void onYes(); 

    public abstract void onNo(); 

} 


그리고 이것은 그것을 사용하는 방법은 다음과 같습니다

YesNoDialog dialog = new YesNoDialog(this, "message") 
{ 

    @Override 
    public void onYes() 
    { 
     // do something   
    } 

    @Override 
    public void onNo() 
    { 
     // do something 
    } 
}; 
dialog.show(); 
0
your getting null pointer exception because your directly trying to access the layout. 
tryout some like this 

// in case your in activity class 
setContentView(getLayoutInflater().inflate(R.layout.logindialog,null)); 

// in case of other than activity pass the context and use it like this 

    setContentView(((LayoutInflator)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE))getLayoutInflater().inflate(R.layout.logindialog,null));