0

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");
venkyMCA
  • 57
  • 1
  • 2
  • 13
  • [**Please See this Blog. This Can Help You**](http://startandroiddevelopment.blogspot.in/2013/11/how-to-pass-boolean-int-string-integer.html) –  Nov 04 '13 at 04:51

6 Answers6

1

Change this

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

to

String msgBody=bundle.getString("msg");
5hssba
  • 8,079
  • 2
  • 33
  • 35
1

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");   
}         
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

in Android Bundle we put key value pair, and it is mandatory that you pass same key, while getting data from bundle, which you are putting data. check your code you are putting two string on intent, which are: "from", and "msg" and you are getting values from intent by key: "from" and "body"

so change it either in starting activity or Activity 2. so that key values would match.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

Try this.....

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);
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
0

try with this way..

Bundle bu=getIntent().getExtras();

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

String msg=bu.get("body").toString();

Gandhi Ishan
  • 149
  • 4
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");