0

我想将值从一个活动发送到另一个活动,但出现空指针异常,请解决我的问题。第一个活动包含短信,该短信的详细信息
将根据这些值发送到第二个活动第二个活动搜索联系人号码并发送回复短信。

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

6 回答 6

1

改变这个

String msgBody=bundle.getString("body");

String msgBody=bundle.getString("msg");
于 2012-04-07T05:47:42.350 回答
1

尝试这个:

@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");   
}         
}
于 2012-04-07T05:48:08.170 回答
1

在 Android Bundle 中,我们放置了键值对,并且您必须传递相同的键,同时从您放置数据的 bundle 中获取数据。检查您的代码,您将两个字符串放在意图上,它们是:“from”和“msg”,并且您通过键从意图中获取值:“from”和“body”

所以在开始活动或活动 2 中更改它。以便键值匹配。

于 2012-04-07T05:53:22.947 回答
0

Try this..... to pass value from Activity1 to Activity2.

    Intent myIntent = new Intent(Activity1.this, Activity2.class);
           myIntent.putExtra("UserId",UserId);
           myIntent.putExtra("UserName",UserName);
           myIntent.putExtra("CompanyID",CompanyID);
           myIntent.putExtra("CompanyName",CompanyName);
           myIntent.putExtra("ProjectId",ProjectId);
           startActivity(myIntent);  

Also can extract the value you can use

    Intent intent = getIntent();
    UserId=intent.getStringExtra("UserId");
    UserName=intent.getStringExtra("UserName");
    CompanyID=intent.getStringExtra("CompanyID");
    CompanyName=intent.getStringExtra("CompanyName");
于 2012-04-09T06:18:10.927 回答
0

尝试这个.....

Activity1.java

Intent intent = new Intent(getApplication(),Activity2.class);
intent.putExtra("from",from);
intent.putExtra("Body",Body);
StartActivity(intent);

Activity2.java

Intent intent = getintent();
Bundle bundle=intent.getExtras();
String Body=bundle.getString("Body");
String From=bundle.getString("From");
setResult("RESULT_OK",intent);
于 2012-04-07T06:09:18.787 回答
0

用这种方法试试。。

捆绑 bu=getIntent().getExtras();

String title=bu.get("from").toString();

字符串 msg=bu.get("body").toString();

于 2012-04-07T07:45:53.473 回答