android - How to change text on a Textview -


evening :)

i have little problem...

as can see in code below, textview trying settext on, getting me nullpointer :(

is there can tell me doing wrong ? :)

and string s isnt null or, view itself. working when setting text in oncreate().

thanks..

public class smsshower extends activity{      private textview status;     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.show);         //status = (textview)findviewbyid(r.id.status);          status = (textview)findviewbyid(r.id.status);     }      public void displaytext(string s){          status.settext(s);      }  } 

smsreceiverclass:

public class smsreceiver extends broadcastreceiver {       private string str;      private boolean received = false;      private smsshower smsshow;      @override     public void onreceive(context context, intent intent) {            //---get sms message passed in---         bundle bundle = intent.getextras();                smsshow = new smsshower();          smsmessage[] msgs = null;         str = "";                     if (bundle != null)         {              object[] pdus = (object[]) bundle.get("pdus");             msgs = new smsmessage[pdus.length];                 (int i=0; i<msgs.length; i++){                  msgs[i] = smsmessage.createfrompdu((byte[])pdus[i]);                   if(msgs[i].getdisplayoriginatingaddress().equals("1990"))                 {                     received = true;                     str += msgs[i].getmessagebody().tostring();                      smsshow.displaytext(str);                    } 

layout:

<?xml version="1.0" encoding="utf-8"?> <relativelayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent">  <textview android:id="@+id/status" android:layout_alignparenttop="true" android:text="status:" android:layout_height="match_parent" android:layout_width="match_parent"></textview> </relativelayout> 

error log:

02-18 01:14:20.064: error/androidruntime(18964): caused by: java.lang.nullpointerexception 02-18 01:14:20.064: error/androidruntime(18964):     @ com.sms1990.smsshower.displaytext(smsshower.java:36) 02-18 01:14:20.064: error/androidruntime(18964):     @ com.sms1990.smsreceiver.onreceive(smsreceiver.java:47) 02-18 01:14:20.064: error/androidruntime(18964):     @ android.app.activitythread.handlereceiver(activitythread.java:2941) 

add basic null check follows:

public void displaytext(string s){   if (status != null)     status.settext(s);  } 

you'll not npe. how start activity , reference in broadcast reciever?


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -