从一个活动传递到另一个值 [英] pass values from one activity to another

查看:121
本文介绍了从一个活动传递到另一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个活动发送的值到另一个,但我得到空指针
   例外请解决我problem.the第一个活动包含SMS的结果的细节
   短信发送到基于这些值个esecond活动搜索联系第二项活动
   没有发送回复短信。

 公共无效的onReceive(上下文的背景下,意图意图)
    {
        //从意向短信地图
        捆绑包= NULL;
        捆绑额外= intent.getExtras();        串消息=;
        字符串的地址= NULL,身体= NULL;        如果(临时演员!= NULL)
        {
            //获取收到的短信阵列
            [对象] smsExtra =(对象[])extras.get(的PDU);            //获取ContentResolver的对象推加密短信传入的文件夹
            // ContentResolver的ContentResolver的= context.getContentResolver();            的for(int i = 0; I< smsExtra.length ++ I)
            {
                SmsMessage短信= SmsMessage.createFromPdu((字节[])smsExtra [I]);                。身体= sms.getMessageBody()的toString();
               地址= sms.getOriginatingAddress();                消息+ = +地址+从短信:\\ n;
                消息+ =身体+\\ n;                //在这里你可以添加任何你code与接收到的SMS工作
                //我加加密所有收到的短信
            }            //显示短信
            Toast.makeText(背景下,消息Toast.LENGTH_SHORT).show();
            意图I =新意图(背景下,AlertActivity.class);           bundle.putString(从,地址);
            bundle.putString(msg中,体);
            i.putExtras(包);            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(ⅰ);        }}

活性2:

 意向书I = getIntent();
    捆绑包= i.getExtras();
    字符串fromAdd = bundle.getString(从);
    字符串msgBody = bundle.getString(身体);


解决方案

试试这个:

  @覆盖
 公共无效的onCreate(捆绑savedInstanceState){
 super.onCreate(savedInstanceState);
 的setContentView(R.layout.main);
 束束= this.getIntent()getExtras()。
如果(捆绑!= NULL)
{
 字符串fromAdd = bundle.getString(从);
 字符串msgBody = bundle.getString(身体);
}
}

i would like to send the values from one activity to another but i got null pointer exception please solve my problem.the first activity contains sms the details of that
sms is send to second activity based on these values th esecond activity search contact no and send reply sms.

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

        }

}

Activity2:

  Intent i=getIntent();
    Bundle bundle=i.getExtras();
    String fromAdd=bundle.getString("from");
    String msgBody=bundle.getString("body");

解决方案

try this:

@Override  
 public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.main);    
 Bundle bundle = this.getIntent().getExtras();   
if (bundle != null)
{       
 String fromAdd = bundle.getString("from");  
 String msgBody = bundle.getString("body");   
}         
}

这篇关于从一个活动传递到另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆