title: place Speech recognition void inside Fragment and then call from MainActivity app crash
body: <p>i have one MainActivity and one Fragment, the Fragment include promptSpeechInput() and onActivityResult() voids. my plan is to place those speech to text recognition code into the fragment then call them with function fro the MainActivity, i did it and got no error when i run it but when i execute the void in the app. It crashes and i dont know why, please help. </p>
  
  <p>MainActivity Code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements FragmentSideButton.Headline {
  
      Button btn;
      Speech speech = new Speech(this);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          btn = (Button)findViewById(R.id.Btn);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  turnOnMic();
              }
          });
     }
  
     @Override
      public void turnOnMic() {
          speech.promptSpeechInput();
      }
  }
  </code></pre>
  
  <p>Speech Fragment code:</p>
  
  <pre><code>@SuppressLint("ValidFragment")
  public class Speech extends Fragment{
      Context context;
  
      public Speech(Context context) {
          this.context = context;
      }
  
  
      public void promptSpeechInput(){
          Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
          i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
          i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault());
          i.putExtra(RecognizerIntent.EXTRA_PROMPT,"say something");
  
          try {
              startActivityForResult(i, 100);
          }catch(ActivityNotFoundException a){
              Toast.makeText(context,"sorry your device does not support speech to text ",Toast.LENGTH_LONG).show();
          }
      }
  
      @Override
      public void onActivityResult(int request_code,int result_code,Intent i){
          super.onActivityResult(request_code,result_code,i);
  
          switch (request_code)
          {
              case 100: if(result_code == RESULT_OK &amp;&amp; i != null){
                  ArrayList&lt;String&gt; result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                  //resultText.setText(result.get(0));
                  Toast.makeText(context,result.get(0),Toast.LENGTH_SHORT).show();
              }
                  break;
          }
      }
  }
  </code></pre>