我试图实现类似于FMX.Advertising.Android
中完成的事情。
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<JAttentiveListenerClass, JAttentiveListener>) 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;
在UI线程中调用构造函数,并在调用父代的构造函数期间抛出NullPointerException
。日志中有以下消息:
我试图逐步调试,然后进入TJavaLocal
的c-tor。此c-tor的以下声明中出现异常:
FLocalRefObjectID := AJNIEnv^.CallObjectMethodA(AJNIEnv, AJNIObject, CreateProxyClass, PJNIValue(ArgsToJNIValues([ClsID, Self])));
HandleJNIException(AJNIEnv); // NullPointerException is here
为什么同样的事情在FMX.Advertising.Android
中起作用并且在我的代码中不起作用? (我正在尝试创建一个实现界面的侦听器,并在AdsListener
FMX.Advertising.Android
中执行所有操作。)
答案 0 :(得分:2)
更加注意日志输出。 NullPointerException
前面有&#39; dvmFindClassByName拒绝X&#39;,这意味着 dalvik无法找到听众类。也许它在classes.dex文件中不存在,也许您在[JavaSignature('com/pack/AttentiveListener')]
中指定了错误的签名。