como fazer referência a uma classe superior dentro de uma classe anônima
-
19-09-2019 - |
Pergunta
Eu tenho esse código:
public class Home extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
//at some point I have
s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
ContextNotionLevel ctnl=new ContextNotionLevel(this);
// <-- how can I reference Home class here to replace **this**, which as it is points to OnSeekBarChangeListener
}
}
}
Solução
Você pode tentar:
ContextNotionLevel ctnl=new ContextNotionLevel(Home.this);
Outras dicas
Você pode usar Home.this
para se referir ao objeto inicial.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow