我是java中的新手。我陷入了这个问题。一切看起来都很好。 代码:内部回调中的代码未运行
testCall.java
package com.example.callbacktest;
public interface testcall {
public void onEvent();
}
testLibrary.java
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");
}
}
MainActivity.java
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");
}
});
}
}
系统日志只打印“出库”。它也应该打印“从onEvent OUT”。 回调中的代码没有运行。我不明白我在这里失踪
我应该在哪里添加此?在构造函数中? – NeiL 2014-10-31 07:13:34
这是你的决定。只要确保在此之前你已经为'listener'分配了一个值。 – laalto 2014-10-31 07:15:36
啊,谢谢你的时间:) – NeiL 2014-10-31 07:17:25