有一個類執行另一個類中創建的接口,代碼如下:
public class MainActivity extends Activity implements AsyncClassSocket.Listener{
AsyncClassSocket thesocketclass = new AsyncClassSocket();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thesocketclass.registerListener(this);
//More codes
}
}
@Override
public void onReplyFromServer(boolean state) {
//Codes
}
上面的代碼沒有問題,但是如果我將代碼放到一個內部方法中:
showProgressDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
thesocketclass = new AsyncClassSocket();
thesocketclass.registerListener(this); <---- Problem
showProgressDialog();
thesocketclass.execute();
}
});
}
就不能再關聯到接口,而是關聯到View.OnClickListener()
。
thesocketclass.registerListener(MainActivity.this); <---- Problem