0

I have created a custome dialog and in that dialog i declare two buttons for image upload but if i cant go to this dialog because of below error

Process: com.example.crowderia.eatitserver, PID: 23218 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.crowderia.eatitserver.HomeActivity.showDialog(HomeActivity.java:135) at com.example.crowderia.eatitserver.HomeActivity.access$000(HomeActivity.java:46) at com.example.crowderia.eatitserver.HomeActivity$1.onClick(HomeActivity.java:87) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20300) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:210) at android.app.ActivityThread.main(ActivityThread.java:5839) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

if I remove setOnclick listner for buttons it show the dialog How can I fix this please help me Im new

private void showDialog() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
        alertDialog.setTitle("Add new category");
        alertDialog.setMessage("Please fill full information");

        LayoutInflater inflater = this.getLayoutInflater();
        View addMenuLayout = inflater.inflate(R.layout.add_new_menu_layout, null);

        edtName = (MaterialEditText) addMenuLayout.findViewById(R.id.edt_name);
        btnSelect = (Button) findViewById(R.id.btn_select);
        btnUpload = (Button) findViewById(R.id.btn_upload);

        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });

        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });

        alertDialog.setView(addMenuLayout);
        alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);

        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if(newCategory != null) {
                    category.push().setValue(newCategory);
                }

            }
        });
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.show();
    }
Hemal
  • 103
  • 3

3 Answers3

3

Your buttons are inflate in addMenuLayout

btnSelect = (Button)addMenuLayout. findViewById(R.id.btn_select);
btnUpload = (Button)addMenuLayout. findViewById(R.id.btn_upload);
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

You have to find id by alert dialogue view because your view exists in the alert dialogue.

btn_one=(Button)addMenuLayout.findViewById(R.id.btn);

you have to do something like this.

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
0

Get the button from parent view.

Button btnSelect = (Button)addMenuLayout. findViewById(R.id.btn_select);
Button btnUpload = (Button)addMenuLayout. findViewById(R.id.btn_upload);
ADM
  • 20,406
  • 11
  • 52
  • 83