2
I’m doing an exercise in Handler just to test how it works, but I’m trying some problem that I can’t understand.
This is my master class code:
public class MainActivity extends Activity {
protected static final int MENSAGEM_TESTE = 1;
private Handler handler = new TesteHandler();
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setText("Atualizar o texto em 3 segundos");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
//Cria msg com delay de 3 seg
Message msg = new Message();
msg.what = MENSAGEM_TESTE;
//Envia msg
handler.sendMessageDelayed(msg, 3000);
}
});
}
}
And this is the code of my class TesteHandler
:
import static com.exercicios.handler.MainActivity.MENSAGEM_TESTE;
public class TesteHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MENSAGEM_TESTE:
Toast.makeText(MainActivity.this, "A mensagem chegou!", Toast.LENGTH_SHORT).show();
break;
}
}
}
Android Studio is returning me the following error:
'com.exercicios.Handler.Mainactivity' is not an enclosing class.
I’m not finding what I need to fix, can anyone give me a help, please? Thanks already!