I have an library written using go mobile and it should has only one callback but when trying implement it, I get two additional methods.
@Override
public Seq.Ref ref() {
return null;
}
@Override
public void call(int i, Seq seq, Seq seq1) {}
Question is, which is right way to implement callback from go on Android Activity?
Right now i have next:
public class MainActivity extends Activity implements implements Mobile.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Mobile.Client client = Mobile.New("192.168.2.1", 9000, this);
try {
client.Connect();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void OnMessage(String s) {
Log.e("GO", s);
}
@Override
public Seq.Ref ref() {
return null;
}
@Override
public void call(int i, Seq seq, Seq seq1) {
}
}
Connection is established successfully but on callback to activity i getting:
panic: runtime error: invalid memory address or nil pointer dereference
If someone can help I'll be really appreciate.
What is the Go source you are binding? (The package mobile and Callback interface)
For passing the Java class that implements Go interface type, see the section "Passing target language objects to Go" of https://godoc.org/golang.org/x/mobile/cmd/gobind
Basically, the generated Java interface type is not meant to be used directly. Instead, the Java class should extends the generated Java interface's Stub class.
Use Mobile.Callback.Stub
instead of Mobile.Callback
for android
...
Mobile.Client client = Mobile.New("192.168.2.1", 9000, new Callbacks());
...
class Callbacks extends Mobile.Callback.Stub {
@Override
public void OnMessage(String s) {
....
}
}