title: Delphi XE6 - How to implement an Android interface and create a listener?
body: <p>I tried to implement a thing similar to the one done in <code>FMX.Advertising.Android</code>.  </p>
  
  <pre><code>type
    // Forward declaration
    JAttentiveListener = interface;   //com.pack.AttentiveListener
    //...
    JAttentiveListenerClass = interface(IJavaClass)
    ['{28A2CA13-A965-4EAB-A4F0-481E20C9AF2A}']
    end;
  
    [JavaSignature('com/pack/AttentiveListener')]
    JAttentiveListener = interface(IJavaInstance)
    ['{45D40262-E5C2-4650-B64A-4C5D56EA6107}']
      {Methods}
      procedure onClicked; cdecl;
      procedure onNotReceived(message1: JString); cdecl;
      procedure onReceived; cdecl;
    end;
    TJAttentiveListener = class(TJavaGenericImport&lt;JAttentiveListenerClass, JAttentiveListener&gt;) end;
  
    TMyListener = class(TJavaLocal, JAttentiveListener)     // JAttentiveListener is Android interface imported above using JNI
    private
      FObj: TCallbackObj;
    public
      constructor Create(Obj: TCallbackObj);
      destructor Destroy; override;
      procedure onClicked; cdecl;
      procedure onNotReceived(message1: JString); cdecl;
      procedure onReceived; cdecl;
    end;
  
  {TMyListener}
  
  constructor TMyListener.Create(Obj: TCallbackObj);
  begin
    inherited Create;   // exception here.
    FObj := Obj;
  end;
  </code></pre>
  
  <p>Constructor is called in UI thread and throws <code>NullPointerException</code> during the call to a parent's constructor. There are following messages in a log:
  <img src="https://i.stack.imgur.com/s5sam.png" alt="[dvmFindClassByName rejecting, java.lang.NullPointerException"></p>
  
  <p>I've tried to debug step-by-step, and stepped into <code>TJavaLocal</code>'s c-tor. Exception occurs in the following statement of this c-tor:</p>
  
  <pre><code>FLocalRefObjectID := AJNIEnv^.CallObjectMethodA(AJNIEnv, AJNIObject, CreateProxyClass, PJNIValue(ArgsToJNIValues([ClsID, Self])));
  HandleJNIException(AJNIEnv);    // NullPointerException is here
  </code></pre>
  
  <hr>
  
  <p>Why does the same thing work in <code>FMX.Advertising.Android</code> and doesn't work in my code? (I'm trying to create a listener implementing an interface and doing everything like in <code>AdsListener</code> from <code>FMX.Advertising.Android</code>.)</p>