片段无法转换为Context [英] Fragment cannot be converted to Context

查看:295
本文介绍了片段无法转换为Context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用片段(android dev的新手),我正在尝试设置一个微调器。目前我对上下文感到很困惑,似乎无法解决这个错误:

This is my first time using fragments (new to android dev) and I'm trying to set up a spinner. At the moment I am quite confused about context and can't seem to solve this error:

Error:(52, 78) error: incompatible types: HotkeysFragment cannot be converted to Context

以下是其引用的代码:

HotkeysFragment.java

HotkeysFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    Spinner hotkey_selector_spinner = (Spinner) rootView.findViewById(R.id.hotkey_selector_spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.hotkey_options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hotkey_selector_spinner.setAdapter(adapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootViewB = inflater.inflate(R.layout.fragment_hotkeys, container, false);
    rootView = rootViewB;
    return rootViewB;
}

具体行是:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.hotkey_options, android.R.layout.simple_spinner_item);

进口:

import android.app.Activity; 
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.zip.Inflater;

我该如何解决这个问题?

How can I fix this?

推荐答案

来自此处:


警告:如果在Fragment中需要Context对象,则可以
调用getActivity()。但是,当片段附加到活动时,请小心调用getActivity()仅
。当片段不是附加的
,或者在其生命周期结束时被分离时,
getActivity()将返回null。

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.

所以,除了将这个更改为 getActivity()之外,我还建议你工作使用 getActivity() in onActivityCreated()(因为您还需要首先对视图进行充气)

So, in addition to changing this to getActivity(), I also suggest that you work with getActivity() in onActivityCreated() (since you also need the view to be inflated first)

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    Spinner hotkey_selector_spinner = (Spinner) getView().findViewById(R.id.hotkey_selector_spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.hotkey_options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hotkey_selector_spinner.setAdapter(adapter);
}

这篇关于片段无法转换为Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆