Why can't I override onConfigurationChanged(Configuration)?

拟墨画扇 提交于 2020-01-14 12:43:28

问题


I've already seen the similar question here, and I have already added the line import android.content.res.Configuration;. It hasn't helped though.

I am writing a class that extends AdapterView<Adapter>, and Eclipse will not let me override onConfigurationChanged(Configuration). As seen in the AdapterView page in the Android Docs, the method does indeed exist. So why can't I override it?

Here is my implementation:

import android.content.Context;
import android.content.res.Configuration;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
public class Foo extends AdapterView<Adapter> {

  public Foo(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }

  @Override
  public Adapter getAdapter() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public View getSelectedView() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void setAdapter(Adapter adapter) {
    // TODO Auto-generated method stub

  }

  @Override
  public void setSelection(int position) {
    // TODO Auto-generated method stub

  }

  /*
   * Error is thrown here... The method onConfigurationChanged(Configuration) of
   * type Foo must override or implement a supertype method
   */
  @Override
  protected void onConfigurationChanged(Configuration newConfig) {
    /*
     * Error is thrown here... The method onConfigurationChanged(Configuration) is
     * undefined for the type AdapterView<Adapter>
     */
    super.onConfigurationChanged(newConfig);
  }
}

回答1:


onConfigurationChanged() was added to View in API Level 8. Your build target in Eclipse (or the target in default.properties for the command line) probably is set to a lower API level.




回答2:


The onConfigurationChanged is in the View only "Since: API Level 8". If you are developing against an earlier version, there is no such method, so you can't override and call them. There is one in "activity", and you can prove your code to be correct by changing your extends into activity. This will not work for compiling ofcourse since you want an adapter, but as a proof of concept for your imports and all it will :)



来源:https://stackoverflow.com/questions/6402512/why-cant-i-override-onconfigurationchangedconfiguration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!