如何更改Textview上的文本

时间:2011-02-18 00:20:18

标签: android textview

晚上:)

我有点问题......

正如你在下面的代码中看到的那样,我试图设置文本的Textview正在给我一个Nullpointer :(

有没有人可以告诉我我做错了什么? :)

字符串s不是null,或者是视图本身。在oncreate()中设置文本时它正在工作。

谢谢..

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);



                }

布局:

<?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>

错误日志:

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)

4 个答案:

答案 0 :(得分:4)

displayText在哪里被调用?是否有可能在onCreate之前(或至少在onCreate完成之前)调用它?如果是,您将获得NullPointerException,因为尚未设置status

答案 1 :(得分:0)

添加基本空检查,如下所示:

public void displayText(String s){
  if (status != null)
    status.setText(s);

}

你不会得到NPE。你如何开始活动并在广播接收器中获得参考?

答案 2 :(得分:0)

我试图检查onCreate被调用的时间......

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!!!!!");
    }

我在onCreate中创建了一个setText,当应用程序发送短信时:

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); 

pendingIntent负责启动SmsShower并调用oncreate。

因此smsreceiver在调用oncreate之后调用displayText()如果我正确理解..

但仍然是那个frigin nullpointer

答案 3 :(得分:0)

当我看到sms.sendTextMessage("1990", null, s, pi, null)函数的第四个参数时,pi PendingIntent将在邮件发送后启动你的smsShower Activity。

我看不到广播接收器中捕获的意图来自哪里?你不能直接在smsShower onCreate()中从Intent解析“pdus”吗? (特别是当pi PendingIntent启动Activity而不是触发BroadcastReceiver时,如代码中所述。)

此外,我对您在onReceive()函数中引用smsShower活动的方式感觉不好。您在函数的第二行创建了一个新的smsShower,但这并不能保证它与先前启动的Activity相同。它实际上或多或少是一种保证相同的活动。