我想将值从一个活动发送到另一个活动,但出现空指针异常,请解决我的问题。第一个活动包含短信,该短信的详细信息
将根据这些值发送到第二个活动第二个活动搜索联系人号码并发送回复短信。
public void onReceive( Context context, Intent intent )
{
// Get SMS map from Intent
Bundle bundle = null;
Bundle extras = intent.getExtras();
String messages = "";
String address = null,body=null;
if ( extras != null )
{
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( "pdus" );
// Get ContentResolver object for pushing encrypted SMS to incoming folder
//ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
body = sms.getMessageBody().toString();
address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
// Here you can add any your code to work with incoming SMS
// I added encrypting of all received SMS
}
// Display SMS message
Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
Intent i=new Intent(context,AlertActivity.class);
bundle.putString("from",address);
bundle.putString("msg",body);
i.putExtras(bundle);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
活动2:
Intent i=getIntent();
Bundle bundle=i.getExtras();
String fromAdd=bundle.getString("from");
String msgBody=bundle.getString("body");