I'm doing a Handler exercise just to test how it works, but I'm trying some problem I can not figure out.
This is my main 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 for 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 someone please give me a hand? Thank you in advance!