android - Can't change a Custom DialogFragment layout dynamically -


so reading through google api guides came across how load custom layout within alert dialog box here.

i wrote class extends dialogfragment class this:

    string product; string description; string company; public void getadinfo(string p, string d, string c) {     product = p;     description = d;     company = c; } public dialog oncreatedialog(bundle savedinstancestate) {     alertdialog.builder builder = new alertdialog.builder(getactivity());     layoutinflater inflater = getactivity().getlayoutinflater();     builder.setview(inflater.inflate(r.layout.ad_dialog, null));             textview pt = (textview) getview().findviewbyid(r.id.product);             pt.settext(product);             textview dt = (textview) getview().findviewbyid(r.id.description);             dt.settext(description);             textview ct = (textview)getview().findviewbyid(r.id.company);             ct.settext(company);            builder.setpositivebutton("ok", new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                    //                }            });            builder.setnegativebutton("exit", new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                    dismiss();                }            });     return builder.create(); } 

}

here's layout:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <textview     android:id="@+id/product"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:textisselectable="false"    /> <textview     android:id="@+id/company"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:textisselectable="false"    />   <textview     android:id="@+id/description"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:textisselectable="false"    /> 

i instanciate dialog within onclicklistener these lines.

addialogfragment ad = new addialogfragment();                         ad.getadinfo(j.getattribute("product"), j.getattribute("description"), j.getattribute("company"));                         ad.show(getfragmentmanager(), null); 

j being element xml node.

when click on button that's supposed run dialog nullpointerexception error logcat:

e/androidruntime(10483):    @ com.onix.mallard.android.guifrag.addialogfragment.oncreatedialog(addialogfragment.java:29) 

the error refers line:

pt.settext(product); 

ultimately, crashes app completely. how can change layout of dialogfragment dynamically? android api guide says dialogfragments bound same lifecycle fragments, doesn't tell me since don't make use of fragmenttransactions (to knowledge). if not possible , need instance information activity, no harm done.

if helps, dialog called within fragment.

here's how (untested code) ;)

your custom dialog:

public class addialogfragment extends dialogfragment {    string mproduct;    string mdescription;    string mcompany;    textview mproducttv;    textview mdescriptiontv;    textview mcompanytv;     public void setadinfo(string product, string desc, string company)    {        mproduct = product;        mdescription = desc;        mcompany = company;    }    public addialogfragment() {         super();    }    public static addialogfragment newinstance() {         return new addialogfragment();    }  public dialog oncreatedialog(final bundle savedinstancestate) {    layoutinflater factory = layoutinflater.from(getactivity());    final view content = factory.inflate(r.layout.ad_dialog, null);    mproducttv = (textview) content.findviewbyid(r.id.product);    mdescriptiontv = (textview) content.findviewbyid(r.id.description);    mcompanytv = (textview) content.findviewbyid(r.id.company);     filltextviews();     alertdialog.builder dialog;    if (build.version.sdk_int >= build.version_codes.honeycomb) {       dialog = new alertdialog.builder(new contextthemewrapper(getactivity(), r.style.theme.devicedefault.dialog.)); //can't remember name or create custom theme.    } else {       dialog = new alertdialog.builder(getactivity());//anything below honeycomb can die :).    }     //  customize dialog    dialog.settitle(getactivity().getstring(r.string.some_title_if_any))       .setview(content)       .setcancelable(true) //this default think.       .setpositivebutton("ok", new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                    //                }            });       .setnegativebutton("exit", new dialoginterface.onclicklistener() {                public void onclick(dialoginterface dialog, int id) {                    dismiss();                }            });    return dialog.create(); }  private void filltextviews() {    mproducttv.settext(mproduct);    mdescriptiontv.settext(mdescription);    mcompanytv.settext(mcompany); } 

and how call activity example:

public void showyourfancydialog() {    addialogfragment dialog = addialogfragment.newinstance();    dialog.setadinfo(yourproduct, yourdescription, yourcompany);    dialog.show(getsupportfragmentmanager(), "dialog"); } 

using patterns google's documentation


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -