-1

I have a stupid problem but i couldnt solve it yet. I have an activity with a button in toolbar, this button open one dialog with some filter params. Here is what a i did..

Spinner spinner_categoria;
Spinner spinner_vendedor;
SeekBar seek_preco;
TextView tv_filtro;

onCreate method {
 ...
}

 onClickEvent{
     dialogFiltro();
}

    public void dialogFiltro() {
    Context context = SalesActivity.this;
    final Dialog dialog;

    dialog = new Dialog(context);
    //dialog.setContentView(R.layout.dialog_filtro);
    // dialog.setTitle("Filtro");
    //dialog.show();

    VendedorDAO auxVendedor = new VendedorDAO();
    final List<String> listVendedor = auxVendedor.getVendedorList();

    View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null, false);

    CardView bt_cancelar = (CardView) dialogView.findViewById(R.id.cb_filtro_cancelar);
    CardView bt_aceitar = (CardView) dialogView.findViewById(R.id.cb_filtro_aceitar);
    seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);

    tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
    spinner_categoria = (Spinner) dialogView.findViewById(R.id.spinner_categoria);
    spinner_vendedor = (Spinner) dialogView.findViewById(R.id.spinner_vendedor);

    spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
            this, R.array.categoria_array, android.R.layout.simple_spinner_item));
    spinner_vendedor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listVendedor));

    String oi = "oi";

    tv_filtro.setText(oi);

    dialog.setContentView(dialogView);
    dialog.show();

    seek_preco.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // tv_filtro.setText("R$ " + progress);
            // Log.i("RS " , String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    bt_cancelar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();


        }
    });


    bt_aceitar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            categoria = (String) spinner_categoria.getSelectedItem();
            vendedor = (Integer) spinner_vendedor.getFirstVisiblePosition();
            Log.e("id vendedor ", String.valueOf(vendedor));

            if (vendedor == 0) {
                vendedor = -1;
            }


            int precoAux = seek_preco.getProgress();
            precoMax = (float) precoAux;
        }
    });


}

I have this in dialog_filtro.xml file:

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

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:foreground="@drawable/border_card"
        card_view:cardElevation="2dp"
        card_view:cardMaxElevation="8dp">


        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/cb_filtro_cancelar"
                        android:layout_width="70dp"
                        android:layout_height="30dp"
                        android:background="@color/colorPrimary"
                        android:elevation="2dp"
                        android:text="X"
                        android:textColor="@android:color/white" />

                    <Button
                        android:id="@+id/cb_filtro_aceitar"
                        android:layout_width="70dp"
                        android:layout_height="30dp"
                        android:layout_gravity="end"
                        android:background="@color/colorPrimary"
                        android:elevation="2dp"
                        android:text="Aceitar"
                        android:textColor="@android:color/white" />

                </FrameLayout>


                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Filtrar por"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/colorPrimary" />

                <TextView
                    android:id="@+id/tv_label_categoria"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Categoria"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <Spinner
                    android:id="@+id/spinner_categoria"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:spinnerMode="dialog" />

                <TextView
                    android:id="@+id/tv_label_vendedor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Supermercado"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <Spinner
                    android:id="@+id/spinner_vendedor"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:spinnerMode="dialog" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Preço máximo"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <SeekBar
                        android:id="@+id/sb_preco_max"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:indeterminate="false"
                        android:max="100"
                        android:progress="0" />

                    <TextView
                        android:id="@+id/tv_filtro_preco"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:text="5"
                        android:textAppearance="?android:attr/textAppearanceSmall" />
                </LinearLayout>

                <View
                    android:id="@+id/rv_shoppingcart_separator"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginBottom="2dp"
                    android:layout_marginTop="10dp"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_label_oerdenar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="15dp"
                    android:text="Ordenar por"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/colorPrimary" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Preço:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <RadioGroup
                    android:id="@+id/group_preco"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/rb_preco_maior"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="false"
                        android:text="Maior" />

                    <RadioButton
                        android:id="@+id/rb_preco_menor"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="false"
                        android:text="Menor" />
                </RadioGroup>

                <TextView
                    android:id="@+id/tv_label_data_Validade"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:text="Data de validade:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <RadioGroup
                    android:id="@+id/group_validade"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/rb_validade_maior"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="false"
                        android:text="Maior" />

                    <RadioButton
                        android:id="@+id/rb_validade_menor"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="false"
                        android:text="Menor" />
                </RadioGroup>

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

                    <TextView
                        android:id="@+id/tv_label_ofertas"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_weight="1"
                        android:text="Apenas ofertas:"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <CheckBox
                        android:id="@+id/cb_offer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_weight="1"
                        android:checked="false"
                        android:text="Selecionar apenas ofertas" />
                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </android.support.v7.widget.CardView>

</LinearLayout>

Everything works fine (spinner, cardview etc) but i always get NullPointerException because the TextView and i don't know why. Here goes my log:

FATAL EXCEPTION: main
Process: com.catafeira.catafeira, PID: 2419
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.catafeira.catafeira.SalesActivity.dialogFiltro(SalesActivity.java:281)
at com.catafeira.catafeira.SalesActivity.onOptionsItemSelected(SalesActivity.java:432)
at android.app.Activity.onMenuItemSelected(Activity.java:2914)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

EDIT: I know what is a NullPointerException, I already checked the previous posts on the subject, but no problem is the same as mine

2 Answers2

0

I think the problem is with findViewByID. The documentation says

findViewById

Added in API level 1 View findViewById (int id) Finds a child view with the given identifier. Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()).

It is just possible that since you are calling show() later the textView might not be created. I would do this

View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null);

Button bt_cancelar = (Button) dialogView.findViewById(R.id.cb_filtro_cancelar);
Button bt_aceitar = (Button) dialogView.findViewById(R.id.cb_filtro_aceitar);
seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);

tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
spinner_categoria = (Spinner)  dialogView.findViewById(R.id.spinner_categoria);
spinner_vendedor = (Spinner)  dialogView.findViewById(R.id.spinner_vendedor);

dialog.setContentView(dialogView)
dialog.show();

spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
        this, R.array.categoria_array, android.R.layout.simple_spinner_item));
spinner_vendedor.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listVendedor));

tv_filtro.setText("oi");

Change the XML to the following

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:foreground="@drawable/border_card"
    android:layout_margin="8dp"
    card_view:cardElevation="2dp"
    card_view:cardMaxElevation="8dp">



    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/cb_filtro_cancelar"
                    android:layout_width="70dp"
                    android:layout_height="30dp"
                    android:background="@color/colorPrimary"
                    android:elevation="2dp"
                    android:text="X"
                    android:textColor="@android:color/white" />

                <Button
                    android:id="@+id/cb_filtro_aceitar"
                    android:layout_width="70dp"
                    android:layout_height="30dp"
                    android:layout_gravity="end"
                    android:background="@color/colorPrimary"
                    android:elevation="2dp"
                    android:text="Aceitar"
                    android:textColor="@android:color/white" />

            </FrameLayout>


            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Filtrar por"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/colorPrimary" />

            <TextView
                android:id="@+id/tv_label_categoria"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Categoria"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Spinner
                android:id="@+id/spinner_categoria"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:spinnerMode="dialog" />

            <TextView
                android:id="@+id/tv_label_vendedor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Supermercado"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Spinner
                android:id="@+id/spinner_vendedor"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:spinnerMode="dialog" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Preço máximo"
                android:textAppearance="?android:attr/textAppearanceMedium" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <SeekBar
                    android:id="@+id/sb_preco_max"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:indeterminate="false"
                    android:max="100"
                    android:progress="5" />

                <TextView
                    android:id="@+id/tv_filtro_preco"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:text="5"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </LinearLayout>

            <View
                android:id="@+id/rv_shoppingcart_separator"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginBottom="2dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/darker_gray" />

            <TextView
                android:id="@+id/tv_label_oerdenar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:text="Ordenar por"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/colorPrimary" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Preço:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <RadioGroup
                android:id="@+id/group_preco"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"
                android:gravity="center"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rb_preco_maior"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="Maior" />

                <RadioButton
                    android:id="@+id/rb_preco_menor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="Menor" />
            </RadioGroup>

            <TextView
                android:id="@+id/tv_label_data_Validade"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="Data de validade:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <RadioGroup
                android:id="@+id/group_validade"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rb_validade_maior"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="Maior" />

                <RadioButton
                    android:id="@+id/rb_validade_menor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:text="Menor" />
            </RadioGroup>

            <TextView
                android:id="@+id/tv_label_ofertas"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="10dp"
                android:text="Apenas ofertas:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <CheckBox
                android:id="@+id/cb_offer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:checked="false"
                android:text="Selecionar apenas ofertas" />

        </LinearLayout>

    </ScrollView>

</android.support.v7.widget.CardView>

Hope this helps!

prats110892
  • 6,599
  • 1
  • 12
  • 6
  • Tried, still without sucess, i have updated my full code, check out – Luiz Otavio Nunes Jul 15 '16 at 19:47
  • does it still give a NPE? Also, I made a small edit. Check it out! – prats110892 Jul 15 '16 at 19:54
  • yes, still NPE. I had updated my code with ur sugestion and posted the whole code – Luiz Otavio Nunes Jul 15 '16 at 19:59
  • still the same, if ur change was just the default text i've alreadly did this too ;/... its crazy man...I never made a post before because usually I can solve my problems alone, but this time its difficult... anyway, thanks for trying to help – Luiz Otavio Nunes Jul 15 '16 at 21:06
  • no my changes weren't just the default text.. I changed the weight-age and optimized your layout significantly but still it doesn't make a lot of sense to me. You shouldn't get a null pointer on that if you are inflating the layout using layout inflater. – prats110892 Jul 15 '16 at 21:09
  • hmm ok.. Thanks for the optimization.. but im still getting NPE..makes no sense – Luiz Otavio Nunes Jul 15 '16 at 21:18
0

SOLVED

The problem was something related android material design, I didn't note before that some layouts has 2 files, so some files were outdated -.- Thanks for everyone that tried to help.