java 为什么我不能覆盖 onConfigurationChanged?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2032007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:02:11  来源:igfitidea点击:

Why can't I override onConfigurationChanged?

javaandroid

提问by Ted

I'm trying to override the method onConfigurationChangedand I get the error:

我正在尝试覆盖onConfigurationChanged方法,但出现错误:

The method onConfigurationChanged(Configuration) of type BaseActivity must override or implement a supertype method

BaseActivity 类型的 onConfigurationChanged(Configuration) 方法必须覆盖或实现超类型方法

Here is my BaseActivity.java:

这是我的 BaseActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class BaseActivity extends Activity 
{
    protected View.OnClickListener mButtonListenerUPP;
    protected View.OnClickListener mButtonListenerALT;
    protected View.OnClickListener mButtonListenerNAV;
    protected View.OnClickListener mButtonListenerHIS;

    @Override
    public void setContentView(int layoutResID) 
    {
        super.setContentView(layoutResID);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
      setContentView(R.layout.main);
    }
}

A lot of posts on the Internet are saying that I can override this method... Any ideas?

网上很多帖子都说我可以覆盖这个方法......有什么想法吗?

回答by Christopher Orr

Have you got android.content.res.Configurationin your import statements? Eclipse can insert imports automatically if you press Ctrl+Shift+O.

你有android.content.res.Configuration你的导入语句吗?如果您按 Ctrl+Shift+O,Eclipse 可以自动插入导入。

If that's missing, the compiler will be unable to recognise that you're legitimately overriding the superclass method and so will throw an error.

如果缺少,编译器将无法识别您正在合法地覆盖超类方法,因此会引发错误。

回答by drago-od

Let me guess. SuperNotCalledException.

让我猜猜。SuperNotCalledException。

@Override
public void onConfigurationChanged(Configuration newConfig)
{      
    super.onConfigurationChanged(newConfig); // add this line
    setContentView(R.layout.main);
}

回答by manuel aldana

Maybe there is a problem with the @Override annotation. Are you sure that both methods annotated with @Override are defined in Activity?

可能@Override 注释有问题。你确定在Activity中定义了用@Override注解的两个方法吗?

If not you have to remove the respective @Override annotation.

如果不是,您必须删除相应的 @Override 注释。