Java error in Google Android tutorial "HelloFormStuff"

I am a neophyte. I completed the tutorial at http://developer.android.com/resources/tutorials/views/hello-formstuff.html to add the OnClick button and handler by copying the tutorial code into mine:

public class FormStuff extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageButton button = (ImageButton) findViewById(R.id.android_button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(FormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });
        }
}

In Eclipse, this leads to two errors

Description Resource    Path    Location    Type
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})  FormStuff.java  /FormStuffExample/src/com/example/formstuffexample  line 17 Java Problem
The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int) FormStuff.java  /FormStuffExample/src/com/example/formstuffexample  line 17 Java Problem

What am I doing wrong? Thank!

+3
source share
2 answers

Based solely on error messages ...

You are using (implicitly) the wrong OnClickListenerinterface / class. It looks like there are two: View.OnClickListener and DialogInterface.OnClickListener.

The solution is to fully qualify your anonymous OnClickListener.

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(FormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });
+9
source

, . , . Eclipse , , . . Eclipse , O, :

import android.view.View.OnClickListener;
+1

Source: https://habr.com/ru/post/1726573/


All Articles