title: how to get back message in android using intent and startActivityfor result?
body: <p>i am creating android app that allow the user to get to the second activity using intent and type a string in  the edit text that the system will get back the string as a toast but the problem is that the system force close .</p>
  
  <p>can anyone help me with this problem ???</p>
  
  <h1>MainActivity.java</h1>
  
  <pre><code>package com.devleb.intentmenudemoapp;
  
  import android.app.Activity;
  import android.app.SearchManager;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity implements OnClickListener {
  
      private static final int INTENT_GET_MSG = 1;
  
      TextView txt1, txt2;
      EditText edittxt1, edittxt2;
      Button btn1, btn2, btn3, btn4;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          txt1 = (TextView) findViewById(R.id.txt1);
  
          txt2 = (TextView) findViewById(R.id.txt2);
  
          edittxt1 = (EditText) findViewById(R.id.edit1);
  
          edittxt2 = (EditText) findViewById(R.id.edit2);
  
          btn1 = (Button) findViewById(R.id.btn);
  
          btn1.setOnClickListener(this);
  
          btn2 = (Button) findViewById(R.id.btn2);
  
          btn2.setOnClickListener(this);
  
          btn3 = (Button) findViewById(R.id.btn3);
  
          btn3.setOnClickListener(this);
  
          btn4 = (Button) findViewById(R.id.btn4);
  
          btn4.setOnClickListener(this);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
  
          String value;
  
          switch (v.getId()) {
          case R.id.btn:
  
              value = edittxt1.getText().toString();
              txt2.setText(value);
  
              break;
  
          case R.id.btn2:
              value = edittxt1.getText().toString();
              Intent i = new Intent(this, SecondActivity.class);
              i.putExtra("msgFromFirstActivity", value);
              startActivity(i);
              break;
          case R.id.btn3:
              Intent ii = new Intent();
              ii.setType("image/pictures/*");
              ii.setAction(Intent.ACTION_GET_CONTENT);
              startActivity(ii);
              break;
          case R.id.btn4:
              Intent iii = new Intent(this, SecondActivity.class);
              startActivityForResult(iii, INTENT_GET_MSG);
  
          default:
              break;
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // TODO Auto-generated method stub
  
          switch (requestCode) {
          case INTENT_GET_MSG:
  
              if (resultCode == Activity.RESULT_OK) {
                  String returnMSG = data.getExtras().getString(
                          SecondActivity.RETURN_MSG);
                  Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
  
              }
              Toast.makeText(this, "Error!!!", Toast.LENGTH_SHORT).show();
  
              break;
  
          default:
              break;
          }
  
          super.onActivityResult(requestCode, resultCode, data);
      }
  
  }
  </code></pre>
  
  <h1>SecondActivity.java</h1>
  
  <pre><code>package com.devleb.intentmenudemoapp;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class SecondActivity extends Activity {
  
  
      public static   String RETURN_MSG = "return_msg";
      TextView txt;
      EditText edittxt;
      String result, editResult;
      Button btn;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
  
          edittxt = (EditText)findViewById(R.id.edit_txt);
          btn = (Button)findViewById(R.id.btn);
          btn.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
              editResult = edittxt.getText().toString();
              Intent i = new Intent();
              i.putExtra(RETURN_MSG, editResult);
  
              setResult(Activity.RESULT_OK, i);
              }
          });
  
          txt = (TextView) findViewById(R.id.txt);
  
          Bundle extras = getIntent().getExtras();
          result = extras.getString("msgFromFirstActivity");
          txt.setText(result);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.second, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <h1>logcat</h1>
  
  <pre><code>12-07 08:59:45.811: E/AndroidRuntime(1504): Caused by: java.lang.NullPointerException
  12-07 08:59:45.811: E/AndroidRuntime(1504):     at com.devleb.intentmenudemoapp.SecondActivity.onCreate(SecondActivity.java:45)
  </code></pre>
  
  <p>this is the line 45
  <strong>result = extras.getString("msgFromFirstActivity");</strong></p>