title: Android communicate Activity with Fragment
body: <p>I don't know what's going on, but I am getting an exception and the app freezes.</p>
  <p><strong>Main activity:</strong></p>
  <pre><code>public class MainAcitivty extends FragmentActivity implements OnClickListener
  {
      // ...
  
      public interface OnItemSelectedListener
      {
          public void onItemSelected(int resourceId);
      }
  
      OnItemSelectedListener mCallback;
  
      @Override
      public void onClick(View v)
      {
          if (v.getId() == R.id.my_imageView)
          {
              mCallback.onItemSelected(R.id.my_imageView);
          }
      }
  }
  </code></pre>
  <p><strong>Fragment:</strong></p>
  <pre><code>public MyFragment extends Fragment implements MainAcitivty.OnItemSelectedListener
  {
     @Override
     public void onItemSelected(int resourceId)
     {
         Log.i(&quot;INFO&quot;, &quot;Selected: &quot; + resourceId);
     }
  }
  </code></pre>
  <p>If I click the selected button, the app freezes and the following Exception is throw:</p>
  <blockquote>
  <p>E/AndroidRuntime(21649): FATAL EXCEPTION: main</p>
  <p>E/AndroidRuntime(21649): java.lang.NullPointerException</p>
  <p>E/AndroidRuntime(21649):  at
  com.example.MainActivity.onClick(MainActivity.java:53)</p>
  <p>E/AndroidRuntime(21649):  at
  android.view.View.performClick(View.java:4232)</p>
  <p>E/AndroidRuntime(21649):  at
  android.view.View$PerformClick.run(View.java:17298)</p>
  <p>E/AndroidRuntime(21649):  at
  android.os.Handler.handleCallback(Handler.java:615)</p>
  <p>E/AndroidRuntime(21649):  at
  android.os.Handler.dispatchMessage(Handler.java:92)</p>
  <p>E/AndroidRuntime(21649):  at android.os.Looper.loop(Looper.java:137)</p>
  <p>E/AndroidRuntime(21649):  at
  android.app.ActivityThread.main(ActivityThread.java:4921)</p>
  <p>E/AndroidRuntime(21649):  at
  java.lang.reflect.Method.invokeNative(Native Method)</p>
  <p>E/AndroidRuntime(21649):  at
  java.lang.reflect.Method.invoke(Method.java:511)</p>
  <p>E/AndroidRuntime(21649):  at
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)</p>
  <p>E/AndroidRuntime(21649):  at
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)</p>
  <p>E/AndroidRuntime(21649):  at dalvik.system.NativeStart.main(Native
  Method)</p>
  </blockquote>