我有一个小问题...
正如你可以在下面我的代码,我正在尝试的setText在TextView的,看到的是越来越我一个空指针:(
有没有人能告诉我,我做错了吗?:)
而且字符串s心不是空,或者是视图本身。它在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)
s字符串从broadcastreceiver传递...并成功传递。我检查了一些log.w的:) – Roskvist 2011-02-18 00:34:00