How to change text on a Textview
Evening :)
I have a little problem...
As you can see in my code below, the Textview which i am trying to setText on, is getting me a Nullpointer :(
Is there anyone which can tell me what i am doing wrong ? :)
And the string s isnt null or, it is the view itself. It is working when setting the text in the 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 the 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];
for (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_wid开发者_StackOverflowth="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): at com.sms1990.smsShower.displayText(smsShower.java:36)
02-18 01:14:20.064: ERROR/AndroidRuntime(18964): at com.sms1990.smsreceiver.onReceive(smsreceiver.java:47)
02-18 01:14:20.064: ERROR/AndroidRuntime(18964): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2941)
Where is displayText
invoked? Is it possible it is being called before onCreate
(or at least before onCreate
has completed)? If it is you will get a NullPointerException because status
hasn't been set yet.
Add a basic null check as follows:
public void displayText(String s){
if (status != null)
status.setText(s);
}
you'll not get NPE. How do you start activity and get a reference to it in broadcast reciever?
I tried to check when onCreate is invoked...
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.setText("NewTEXT!!!!!");
}
i made a setText in onCreate, and when the application sends the sms:
public void sendStatusSMS(String fra,String til)
{
String s = "DSB "+fra+" "+til;
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(sms1990.this, smsShower.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("1990", null, s, pi, null);
The pendingIntent is responsible for starting the SmsShower and by doing so invoking the oncreate.
And therefore the smsreceiver is invoking displayText() after oncreate is called.. if i am understanding correctly..
But still that frigin nullpointer
As I see it the fourth parameter to the sms.sendTextMessage("1990", null, s, pi, null)
function, the pi
PendingIntent, will start your smsShower Activity after the message has been sent.
What I don't see is where the Intent that is caught in your broadcast receiver comes from? Can't you parse the "pdus" from the Intent directly in the smsShower onCreate() instead? (especially as the pi
PendingIntent starts the Activity rather than triggers the BroadcastReceiver as it is stated in your code).
Also I have a bad feeling for the way you get a reference to your smsShower Activity in the onReceive()
function. You create a new smsShower
on the second line within the function but that is no guarantee that it's the same Activity that was previously started. It's actually more or less a guarantee that it's not the same Activity.
精彩评论