【问题标题】:I am getting this strange error on in onAttach(Context)我在 onAttach(Context) 中遇到了这个奇怪的错误
【发布时间】:2016-07-03 15:56:00
【问题描述】:

onAttach 函数中,eclipse 显示错误说明

Fragment 类型中的 onAttach(Activity) 方法不适用 对于参数(上下文)

虽然明明是传递了Context类型变量

import android.content.Context;

public class MyListFragment extends Fragment{
    private OnItemSelectedListener listener;

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rsslist_overview,
            container, false);
        Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            updateDetail("fake");
          }
        });
        return view;
      }

      public interface OnItemSelectedListener {
        public void onRssItemSelected(String link);
      }

      @Override
      public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnItemSelectedListener) {
          listener = (OnItemSelectedListener) context;
        } else {
          throw new ClassCastException(context.toString()
              + " must implemenet MyListFragment.OnItemSelectedListener");
        }
      }

      @Override
      public void onDetach() {
        super.onDetach();
        listener = null;
      }

      // may also be triggered from the Activity
      public void updateDetail(String uri) {
        // create a string just for testing
        String newTime = String.valueOf(System.currentTimeMillis());

        // inform the Activity about the change based
        // interface defintion
        listener.onRssItemSelected(newTime);
      }
}

【问题讨论】:

  • 您正在使用的 API 级别??
  • @UsamaZafar targetSdk 版本为 15
  • 那就是问题所在。你为你的targetsdk使用了错误的函数版本......
  • @UsamaZafar 是的,如果 APIpublic void onAttach(Activity context)
  • 是的,看来你明白了。请将 Rohits 的答案标记为已接受,以便其他人也能从中受益。

标签: java android eclipse android-activity android-context


【解决方案1】:

如果你使用 API

public void onAttach(Context context) {

应该是

public void onAttach(Activity context) {

official docs

注意:

onAttach(Context context) 被添加到 api 23 中。见this

【讨论】:

  • @shruti 如果它有效,那么请不要忘记接受答案。
  • 接受您的回答。再次感谢您的帮助
【解决方案2】:

我遇到了同样的问题,您可以尝试在您的 onAtach 方法中传递 Activity,如下所示:

   @Override
      public void onAttach(Activity activity) {
        super.onAttach(context);
        if (context instanceof OnItemSelectedListener) {
          listener = (OnItemSelectedListener) activity;
        } else {
          throw new ClassCastException(context.toString()
              + " must implemenet MyListFragment.OnItemSelectedListener");
        }
      }

并告诉我它是否有效。 希望有所帮助!

【讨论】:

  • 如果我的回答对您有帮助,您可以接受,当然如果您愿意:)
  • 不,你投票赞成答案。接受是当你点击我的答案时的绿色粗线:),你可以选择只接受一个答案。
猜你喜欢
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
相关资源
最近更新 更多