-1

I want to set a divider in my dialog
I do this:

   if (dialog != null) {
        final int dividerId = dialog.getContext().getResources()
                .getIdentifier("android:id/titleDivider", null, null);
        View divider = dialog.findViewById(dividerId);
        if  (divider != null) {
            divider.setBackground(null);
        }

This is my whole code :

  public void setParamsDialog(String componentName, String componentCode, String unit) {
        dialog.setContentView(R.layout.dialog_consumables);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        dialog.setCancelable(false);
        dialog.show();

        if (dialog != null) {
            final int dividerId = dialog.getContext().getResources()
                    .getIdentifier("android:id/titleDivider", null, null);
            View divider = dialog.findViewById(dividerId);
            if (divider != null) {
                divider.setBackground(null);
            }
        }
        int titleDividerId = dialog.getContext().getResources().getIdentifier("titleDivider", "id", context.getPackageName());
        dialog.getWindow().getDecorView().findViewById(titleDividerId).setVisibility(View.GONE);

        compNameText = (TextView) dialog.findViewById(R.id.component_name);
        compCodeText = (TextView) dialog.findViewById(R.id.component_code);
        compUnitText = (TextView) dialog.findViewById(R.id.amount_unit);
        searchEditText = (EditText) dialog.findViewById(R.id.search);
        amountEditText = (EditText) dialog.findViewById(R.id.edittext_amount);

        mListView = (ListView) dialog.findViewById(R.id.consumables_list);
        searchConsumables = (RelativeLayout) dialog.findViewById(R.id.search_consumables);

        cancelButton = (Button) dialog.findViewById(R.id.negative_btn);
        saveButton = (Button) dialog.findViewById(R.id.positive_btn);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
    }

But on android API 23 I get a null pointers

A logcat logs :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
        at pl.dialog.box.DialogConsumables.setParamsDialog(DialogConsumables.java:85)
        at pl.barcode.readers.BarcodeReader$2.onClick(BarcodeReader.java:198)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kala
  • 61
  • 8

1 Answers1

0

According to error log

Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference

its pointing at

dialog.getWindow().getDecorView().findViewById(titleDividerId).setVisibility(View.GONE);

So i would suggest you to set divider inside dialog_consumables like this

<ListView
        android:id="@+id/consumables_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="2dp" >
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51