自定义对话框中的错误

时间:2014-09-05 06:55:43

标签: android dialog customdialog

为什么setOnClickListener会给出以下错误?

java.lang.NullPointerException
com.example.MainActivity.onClick(MainActivity.java:206)
android.view.View.performClick(View.java:4240)
android.view.View$PerformClick.run(View.java:17721)
.
.
.
MainActivity.java的代码是:

public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View color=findViewById(R.id.vie);
    color.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
if(arg0.getId()==R.id.vie){

        final CustomDialogClass cdc=new CustomDialogClass(this);
        View dialogButton = cdc.findViewById(R.id.view3);

        //**** the blow code that is in Block comment give an error****

        /* dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cdc.dismiss();
            }
        });   */
        cdc.show();  
 }
}
}
CustomDialogClass.java的

代码是:

 public class CustomDialogClass extends Dialog  {

 public Activity c;
 public Dialog d;

public CustomDialogClass(Activity a) {
  super(a);
// TODO Auto-generated constructor stub
this.c = a;
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.custom_dialog);
 setTitle("select");

 }
}

和custom_dialog.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="match_parent"
  android:layout_gravity="center"
  android:layout_margin="40dp"
  android:gravity="center" >

<View
    android:id="@+id/view3"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#24b5ed" />
</RelativeLayout>

有谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

文件:src / CustomDialog.java

public class CustomDialog extends Activity {

    private Button buttonClick;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog_main);

        buttonClick = (Button) findViewById(R.id.buttonClick);

        // add listener to button 
        buttonClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // Create custom dialog object
                final Dialog dialog = new Dialog(CustomDialog.this);
                // Include dialog.xml file
                dialog.setContentView(R.layout.dialog);
                // Set dialog title
                dialog.setTitle("Custom Dialog");

                // set values for custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.textDialog);
                text.setText("Custom dialog Android example.");
                ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
                image.setImageResource(R.drawable.image0);

                dialog.show();

                Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
                // if decline button is clicked, close the custom dialog
                declineButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Close dialog
                        dialog.dismiss();
                    }
                });


            }

        });

    }

}

文件:res / layout / custom_dialog_main.xml

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

    <Button
        android:id="@+id/buttonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Show Custom Dialog" />

</LinearLayout

&GT;

文件:res / layout / dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dp" />

    <TextView
        android:id="@+id/textDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:layout_toRightOf="@+id/imageDialog"/>

     <Button
        android:id="@+id/declineButton"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Submit "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/textDialog"
        android:layout_toRightOf="@+id/imageDialog"
        />

</RelativeLayout>