I've a little problem with my toast in android :/ I want to create a little toast when button1
is going to be activated
here is my code:
MainActivity.java
package com.andruiden.toast;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.andruiden.toast.R;
public class MainActivity extends Activity {
//Klick Listener
class MeinClickListener implements OnClickListener{
public void onClick(View v){
String text = "Es wurde geklickt";
Toast t = Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT);
t.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button beim Hörer registrieren
MeinClickListener t = new MeinClickListener();
Button test = (Button) findViewById(R.id.button1);
test.setOnClickListener(t);
}
}
On the line test.setOnClickListener(t);
i get the error:
The method setOnClickListener(View.OnClickListener) in the type view is not applicable to the arguments
What does it means?
I can change the code to this here:
test.setOnClickListener((android.view.View.OnClickListener) t);
But when i run the .apk on my galaxy s2 it happens nothing.. :/
sorry for my bad english xD
please remove these lines from you import section:
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
these are dialog interfaces
and use this instead:
import android.view.View.OnClickListener;
your class should be:
class MeinClickListener implements OnClickListener
{
public void onClick(View v)
{
String text = "Es wurde geklickt";
Toast t = Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT);
t.show();
}
}
You have subclassed the wrong OnClickListener
(you've got click listener from DialogInterface
).
First of all, get rid of DialogInterface
imports and replace your click listener subclass with:
//Klick Listener
class MeinClickListener implements View.OnClickListener{
@Override
public void onClick(View v){
String text = "Es wurde geklickt";
Toast t = Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT);
t.show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.