Java 回调函数中的代码未运行
我是java的新手。我无意中解决了这个问题。我觉得一切都很好。 代码: testCall.javaJava 回调函数中的代码未运行,java,android,interface,Java,Android,Interface,我是java的新手。我无意中解决了这个问题。我觉得一切都很好。 代码: testCall.java package com.example.callbacktest; public interface testcall { public void onEvent(); } package com.example.callbacktest; public class testLibrary { testcall listener; public t
package com.example.callbacktest;
public interface testcall {
public void onEvent();
}
package com.example.callbacktest;
public class testLibrary {
testcall listener;
public testLibrary(){
}
public void createSession( testcall callback ){
this.listener = callback;
System.out.println("Out from library");
}
}
package com.example.callbacktest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testLibrary obj = new testLibrary();
obj.createSession(new testcall(){
@Override
public void onEvent() {
System.out.println("OUT from onEvent");
}
});
}
}
testLibrary.java
package com.example.callbacktest;
public interface testcall {
public void onEvent();
}
package com.example.callbacktest;
public class testLibrary {
testcall listener;
public testLibrary(){
}
public void createSession( testcall callback ){
this.listener = callback;
System.out.println("Out from library");
}
}
package com.example.callbacktest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testLibrary obj = new testLibrary();
obj.createSession(new testcall(){
@Override
public void onEvent() {
System.out.println("OUT from onEvent");
}
});
}
}
MainActivity.java
package com.example.callbacktest;
public interface testcall {
public void onEvent();
}
package com.example.callbacktest;
public class testLibrary {
testcall listener;
public testLibrary(){
}
public void createSession( testcall callback ){
this.listener = callback;
System.out.println("Out from library");
}
}
package com.example.callbacktest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testLibrary obj = new testLibrary();
obj.createSession(new testcall(){
@Override
public void onEvent() {
System.out.println("OUT from onEvent");
}
});
}
}
系统只记录打印“从库中输出”。它还应该打印“从OneEvent输出”。
回调函数中的代码未运行。我不明白我在这里缺少了什么您缺少了对测试库中的
listener.onEvent()
的调用。您没有在testLibrary
类中调用回调方法
在testLibrary类中像这样更改方法
public void createSession( testcall callback ){
this.listener = callback;
System.out.println("Out from library");
listener.onEvent(); //Added line
}
我应该在哪里添加这个?在构造器里?这是你的决定。在此之前,请确保您已为
侦听器指定了一个值。谢谢,我必须勾选laalto回答,他首先回答,无论如何,谢谢:)