android - NullPointerException in dialog - Strange case -
i have stupid problem couldnt solve yet. have activity button in toolbar, button open 1 dialog filter params. here 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 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) nullpointerexception because textview , don't know why. here goes 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 invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference @ com.catafeira.catafeira.salesactivity.dialogfiltro(salesactivity.java:281) @ com.catafeira.catafeira.salesactivity.onoptionsitemselected(salesactivity.java:432) @ android.app.activity.onmenuitemselected(activity.java:2914) @ android.support.v4.app.fragmentactivity.onmenuitemselected(fragmentactivity.java:403) @ android.support.v7.app.appcompatactivity.onmenuitemselected(appcompatactivity.java:189) @ android.support.v7.view.windowcallbackwrapper.onmenuitemselected(windowcallbackwrapper.java:100) @ android.support.v7.view.windowcallbackwrapper.onmenuitemselected(windowcallbackwrapper.java:100) @ android.support.v7.app.toolbaractionbar$2.onmenuitemclick(toolbaractionbar.java:69) @ android.support.v7.widget.toolbar$1.onmenuitemclick(toolbar.java:169) @ android.support.v7.widget.actionmenuview$menubuildercallback.onmenuitemselected(actionmenuview.java:760) @ android.support.v7.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:811) @ android.support.v7.view.menu.menuitemimpl.invoke(menuitemimpl.java:152) @ android.support.v7.view.menu.menubuilder.performitemaction(menubuilder.java:958) @ android.support.v7.view.menu.menubuilder.performitemaction(menubuilder.java:948) @ android.support.v7.widget.actionmenuview.invokeitem(actionmenuview.java:618) @ android.support.v7.view.menu.actionmenuitemview.onclick(actionmenuitemview.java:139) @ android.view.view.performclick(view.java:5204) @ android.view.view$performclick.run(view.java:21158) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5461) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
edit: know nullpointerexception, checked previous posts on subject, no problem same mine
i think problem findviewbyid
. documentation says
findviewbyid
added in api level 1 view findviewbyid (int id) finds child view given identifier. returns null if specified child view not exist or dialog has not yet been created (for example, via show() or create()).
it possible since calling show() later textview might not created. 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 xml 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 helps!
Comments
Post a Comment