java - Take data from Yahoo Finance and show it in Fragment -


in fragment of android application wanna show simple data yahoo finance price, company name, ticker etc of csv(comma-separated values). have error nullpointerexception when tried set values textview in financefragment. can show me correct way or fix code.

thanks help!

activity.java

        alertdialog.builder builder = new alertdialog.builder(this);          layoutinflater inflater = this.getlayoutinflater();         view dialogview = inflater.inflate(r.layout.dialog_search_ticker, null);         builder.setview(dialogview);          final edittext input = (edittext) dialogview.findviewbyid(r.id.input_ticker);         input.setinputtype(inputtype.type_class_text);          builder.setpositivebutton("search ticker", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 final string searchticker = input.gettext().tostring();                  if(searchticker.matches("")) { //check if edittext empty / if empty show message                     toast.maketext(getapplicationcontext(), "space empty. please try enter again!", toast.length_short).show();                 }else{                     if(new yahooasynctask().execute(searchticker) == null){                         toast.maketext(getapplicationcontext(), "there no such ticker. please try enter again!", toast.length_short).show();                     }else{                         new yahooasynctask().execute(searchticker);                     }                 }             }         });  private class yahooasynctask extends asynctask<string, string, stock> {          linearlayout progress = (linearlayout) findviewbyid(r.id.progress);         linearlayout nointernetconnection = (linearlayout) findviewbyid(r.id.nointernetconnection);         framelayout fragmentcontainer = (framelayout) findviewbyid(r.id.fragmentcontainer);          @override         protected void onpreexecute() {             // sets company info invisible, , progress indicator visible             progress.setvisibility(view.visible);             nointernetconnection.setvisibility(view.invisible);             fragmentcontainer.setvisibility(view.gone);         }          protected stock doinbackground(string... ticker) {             stock stock = null;             if (isnetworkavailable()) {                 try {                     stock = yahoofinanceinfo.getstockdata(ticker[0]);                 } catch (exception e) {                     log.d("yahoo_error", e.tostring());                 }             }             return stock;         }          protected void onpostexecute(stock stock) {             progress.setvisibility(view.gone);             if (stock != null) {                 // create new fragment placed in activity layout                 yahoofragment yahoofragment = yahoofragment.newinstance(stock);                  if (findviewbyid(r.id.fragmentcontainer) != null) {                     getsupportfragmentmanager().begintransaction().replace(r.id.fragmentcontainer, yahoofragment).commit();                 } else {                     getsupportfragmentmanager().begintransaction().add(r.id.fragmentcontainer, yahoofragment).commit();                 }                 fragmentcontainer.setvisibility(view.visible);             } else {                 nointernetconnection.setvisibility(view.visible);             }         }     } 

please post error code sure, guess is happening here:

linearlayout headerprogress = (linearlayout) getview().findviewbyid(r.id.progress_linear_layout); linearlayout nointernetconnection = (linearlayout) getview().findviewbyid(r.id.nointernetconnection); linearlayout fragmentcontainer = (linearlayout) getview().findviewbyid(r.id.fragment_container); 

this code executed before return oncreateview() means getview() returns null.

try pulling out these attributes activity , initializing them before calling

new getkeystatsasynctask().execute(ticker); 

update

this meant in comment:

public class financefragment extends fragment {  textview tvcompany; textview tvcompanyticker; textview tvprice; textview tvpricechange; textview tvprevclose; textview tvopen; textview tvvolume; textview tvavgdailyvolume; textview tvdaysrange; textview tvyearrange; textview tvoneyeartarget; textview tvmarketcap;  stock stock;  string ticker;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view view = inflater.inflate(r.layout.fragment_finance, container, false);      tvcompany        = (textview) view.findviewbyid(r.id.tvcompany);     tvcompanyticker  = (textview) view.findviewbyid(r.id.tvcompanyticker);     tvprice          = (textview) view.findviewbyid(r.id.tvprice);     tvpricechange    = (textview) view.findviewbyid(r.id.tvpricechange);     tvprevclose      = (textview) view.findviewbyid(r.id.tvprevclose);     tvopen           = (textview) view.findviewbyid(r.id.tvopen);     tvavgdailyvolume = (textview) view.findviewbyid(r.id.tvavgdailyvolume);     tvvolume         = (textview) view.findviewbyid(r.id.tvvolume);     tvdaysrange      = (textview) view.findviewbyid(r.id.tvdaysrange);     tvyearrange      = (textview) view.findviewbyid(r.id.tvyearrange);     tvmarketcap      = (textview) view.findviewbyid(r.id.tvmarketcap);     tvoneyeartarget  = (textview) view.findviewbyid(r.id.tvoneyeartarget);       updateview();       // when application start working, search google ticker default.     ticker = "goog";     new getkeystatsasynctask().execute(ticker);      return view; } private void updateview() {     if(stock == null) {     // sets company name, price, etc.         tvcompany.settext("");         tvcompanyticker.settext("");         tvprice.settext("");         tvpricechange.settext("");         tvprevclose.settext("");         tvopen.settext("");         tvavgdailyvolume.settext("");         tvvolume.settext("");         tvdaysrange.settext("");         tvyearrange.settext("");         tvmarketcap.settext("");         tvoneyeartarget.settext("");     } else {      // sets company name, price, etc.         tvcompany.settext(stock.getname());         tvcompanyticker.settext(stock.getticker());         tvprice.settext(stock.getprice());         tvpricechange.settext(stock.getpricechange());         tvprevclose.settext(stock.getprevclose());         tvopen.settext(stock.getopen());         tvavgdailyvolume.settext(stock.getavgdailyvolume());         tvvolume.settext(stock.getvolume());         tvdaysrange.settext(stock.getdaysrange());         tvyearrange.settext(stock.getyearrange());         tvmarketcap.settext(stock.getmarketcap());         tvoneyeartarget.settext(stock.getoneyeartarget());          // if price change positive, set textview green. otherwise red.         if (stock.getpricechangedirection() >= 0)             tvpricechange.settextcolor( getresources().getcolor(r.color.finance_green));         else             tvpricechange.settextcolor( getresources().getcolor(r.color.finance_red));      } } ... 

and change onpostexecute:

protected void onpostexecute(stock stock) {     headerprogress.setvisibility(view.gone);     if (stock != null) {         fragmentcontainer.setvisibility(view.visible);         updateview();     } else {         nointernetconnection.setvisibility(view.visible);     } } 

i did not try code, there no guarantee works


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -