調用對象方法,我有這個簡單的SMS監聽器類:無法從廣播聽衆
package net.albanx.smspack;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ReceiverListener extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
ReceiverActivity addtolist = new ReceiverActivity();
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
addtolist.addSMS(msgs[i].getOriginatingAddress(), "now", msgs[i].getMessageBody().toString());
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
我得到當方法
addtolist.addSMS(msgs[i].getOriginatingAddress(), "now", msgs[i].getMessageBody().toString());
被稱爲零點execption錯誤。此方法是一個活動的一部分:
public void addSMS(String sms_adress, String sms_date, String message)
{
LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
// from Number label
TextView from=new TextView(this);
from.setLayoutParams(lparams);
from.setBackgroundColor(Color.argb(200, 0, 0, 0));
from.setTextAppearance(getApplicationContext(), R.style.from_style);
from.setText(this.getString(R.string.label_from_sms)+":"+ sms_adress);
linearLayout.addView(from);
}
問題是在該行:
TextView from=new TextView(this);
在參照該該空。
我想讓一個短信監聽程序,當收到短信它更新我的運行應用程序的短信列表。看來我不能從廣播監聽器調用活動方法。任何人都可以請解釋我該如何解決這個問題?
但實際上問題不在這裏,它是從監聽器 – albanx
調用活動的方法你得到了什麼錯誤? –
null點執行取得上下文 – albanx