java Fragment 无法转换为 Context

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31966406/
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-11-02 19:25:57  来源:igfitidea点击:

Fragment cannot be converted to Context

javaandroidxmlandroid-fragments

提问by mtmeyer

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:

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

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

Here is the code its referring to:

这是它所指的代码:

HotkeysFragment.java

热键片段.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;
}

The specific line is:

具体线路是:

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

Imports:

进口:

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?

我怎样才能解决这个问题?

回答by Andrei Tudor Diaconu

From the Android docs on Fragments from here:

来自此处的Fragments 上的 Android 文档:

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.

注意:如果您的 Fragment 中需要一个 Context 对象,您可以调用 getActivity()。但是,仅当片段附加到活动时才小心调用 getActivity()。当片段尚未附加或在其生命周期结束时被分离时,getActivity() 将返回 null。

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

因此,除了改变thisgetActivity(),我也建议你一起工作getActivity()onActivityCreated()(因为你还需要在视图中首先膨胀)

@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);
}

回答by M D

Change

改变

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

to

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

Access context in fragment by using getActivity()

通过使用访问片段中的上下文 getActivity()

回答by Rohit5k2

Fragmentcan not be converted into Context, an Activitycan.

Fragment不能转换成Context,一个Activity可以。

So you should change

所以你应该改变

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

to

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

回答by Machado

The correct way to access contextinside any Fragmentis by using the getActivity()method.

访问context任何内部的正确方法Fragment是使用getActivity()方法。

You can also get it directly from the ViewGroupinside onCreateView. If you go this way you won't get nullas you could get from getActivity()(as long as you instantiate it for an user interface view before).

您也可以直接从ViewGroup内部获取onCreateView。如果你这样做,你将不会得到null你能得到的getActivity()只要你之前为用户界面视图实例化它)。

fragment_context = container.getContext();