Alertdialog mit setcontentview

Ich möchte erstellen Sie eine benutzerdefinierte alertdialog.So habe ich den folgenden code.

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>

so, wenn ich versuche, verwenden Sie den benutzerdefinierten button erstellt wie diese

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
    }
});

Er sagt nullpointer exception.
Bitte helfen Sie mit, herauszufinden, was läuft hier falsch.

Danke im Voraus

InformationsquelleAutor krishna kumar | 2013-03-12

Schreibe einen Kommentar