@override error: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

StackOverflow https://stackoverflow.com//questions/21011609

  •  21-12-2019
  •  | 
  •  

سؤال

I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.

This is my main activity.

package com.example.grammer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    Button grammerButton = (Button)findViewById(R.id.grammar);

   grammerButton.setOnClickListener(new View.OnClickListener() {

       @Override
      public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, Grammer.class);
        startActivity(intent);
      }

    });
} 
}

This is my second activity.

package com.example.grammer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;

public class Grammer extends Activity {

    @Override
            public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grammer);

         Button grammerButton = (Button) findViewById(R.id.grammar);


         grammerButton.setOnClickListener(new View.OnClickListener() {

          //    @Override
              public void onClick(View view) {
                finish();
              }

            });
          }
    }

Removing the @override will remove the error, but then app is not working as intended.

هل كانت مفيدة؟

المحلول

Check this

OnClickListener() must override a superclass method?

Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above

Have this import statement

 import android.view.View.OnClickListener;

There is no need to remove @Override Annotation.

Also calling finish() is not necessary. The hardware back button does the job.

When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.

http://developer.android.com/guide/components/tasks-and-back-stack.html

Also if you have a Button with id grammar in activity_grammer.xml it is ok.

Make sure you have a button with id grammar in activity_grammer.xml

You can read the topic id

http://developer.android.com/guide/topics/ui/declaring-layout.html

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.

نصائح أخرى

Change "Compiler Compliance Level" to 1.6. of java from project properties.

Place this code::

Just replace then name of layout and button id by your

   import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alayout);


    Button grammerButton = (Button)findViewById(R.id.aId);

   grammerButton.setOnClickListener(new OnClickListener() {

       @Override
      public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, Grammer.class);
        startActivity(intent);
      }

    });

} 
}

Just replace then name of layout and button id by your

    import android.os.Bundle;
import android.app.Activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class Grammer extends Activity {

    @Override
            public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blayout);

         Button grammerButton = (Button) findViewById(R.id.aId);


         grammerButton.setOnClickListener(new OnClickListener() {

             @Override
              public void onClick(View view) {
                finish();
              }

            });
          }

    }

You are suppose to import the ViewClickListener namespace in your code.

Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.

All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.

Try this..

First, You havn't import OnClickListener

import android.view.View.OnClickListener;

and second one

Button grammerButton = (Button) findViewById(R.id.grammar);

you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top