为什么我不能覆盖onConfigurationChanged方法?

3

我正试图重写方法 onConfigurationChanged,但是出现了以下错误:

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

这是我在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);
    }
}

很多网上的帖子都说我可以重写这个方法...有什么想法吗?
3个回答

9

你的导入语句中是否包含android.content.res.Configuration?如果你按下Ctrl+Shift+O,Eclipse会自动插入导入语句。

如果缺失了这个导入语句,编译器将无法识别你是在合法地重写超类方法,因此会抛出错误。


谢谢,就是这样。不知道为什么Eclipse没有建议我导入那个包 =) - Ted

2
让我猜猜,SuperNotCalledException。
@Override
public void onConfigurationChanged(Configuration newConfig)
{      
    super.onConfigurationChanged(newConfig); // add this line
    setContentView(R.layout.main);
}

0

可能存在 @Override 注释的问题。您确定使用 @Override 注释标记的两个方法都在 Activity 中定义了吗?

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


它们都是Activity中的公共方法:http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged%28android.content.res.Configuration%29 - Christopher Orr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接