单击自定义对话框窗口中的按钮时出现空指针异常

时间:2015-03-28 03:29:05

标签: android

堆栈跟踪:

  Process: com.example.admin.Check_point, PID: 1504
    java.lang.NullPointerException
            at com.example.admin.Check_point.Detail$1.onClick(Detail.java:126)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

单击自定义对话框窗口中的按钮时,我收到空指针异常。

下面我发布了代码并指出了错误行。

Detail.java:

public class Detail extends Activity {

 Button btnAddCount;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.vehicle_detail);

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

 btnAddCount.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Dialog dialog = new Dialog(VehicleDetail.this);

                //setting custom layout to dialog
                dialog.setContentView(R.layout.custom_dialog_layout);
                dialog.setTitle("Add List");

                //adding button click event
                final Button createEditText = (Button) dialog.findViewById(R.id.button);

                final Button customSave=(Button)findViewById(R.id.customSave);
                createEditText.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        count=passenger_count.getText().toString();


                        int i=Integer.parseInt(count);


                        if(i>=0) {
                            do {


                                et = new EditText(VehicleDetail.this);
                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                params.gravity = Gravity.CENTER;

                                ((LinearLayout) dialog.findViewById(R.id.container)).addView(et);

                                db.execSQL("insert into PassengerDetail (PASSENGER_ID) values('" + et.getText().toString() + "')");



                                i--;
                                createEditText.setVisibility(View.INVISIBLE);




                            }while(i>0);

                            Toast.makeText(getApplicationContext(), "Running", Toast.LENGTH_LONG).show();

                        }
                    }
                });


  customSave.setOnClickListener(new OnClickListener() { --->126th line
     @Override
      public void onClick(View v) {

      Toast.makeText(getApplicationContext(), "Running", Toast.LENGTH_LONG).show();



                        dialog.dismiss();
                    }
                });

                dialog.show();
            }


        });

custom_dialog_layout.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" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >


        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        <!-- 2 columns -->
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >



            <Button
                android:id="@+id/button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="100dp"
                android:text="Next"

                />


            <Button
                android:id="@+id/customSave"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="100dp"
                android:text="Save" />



        </TableRow>

       </TableLayout>



    </LinearLayout>

</RelativeLayout>

任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分:0)

应为final Button customSave=(Button)dialog.findViewById(R.id.customSave);

答案 1 :(得分:0)

很可能您的customSave为空。您可以通过在空检查中添加日志语句来验证它,或者只是简单地调试它。

final Button customSave=(Button)findViewById(R.id.customSave);
customSave.setOnClickListener... // <- NPE here

你可以夸大R.layout.custom_dialog_layout布局并在其上调用findView()

final View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout, null )
final Button customSave=(Button) v.findViewById(R.id.customSave);