无法解析 android.support.v4.app.Fragment(版本 21.0.3)Android Studio 中的方法 getContext() [英] Cannot resolve method getContext() in android.support.v4.app.Fragment (version 21.0.3) Android Studio

查看:83
本文介绍了无法解析 android.support.v4.app.Fragment(版本 21.0.3)Android Studio 中的方法 getContext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我似乎无法理解它为什么会发生.我的项目中有一些使用 support v4 library (version 21.0.3) 的片段.例如,在这些片段中,我使用了 getContext() 方法来显示 Toasts .但是昨天突然我项目中的所有 getContext 都坏了,Android Studio 在编译时向我显示了 26 个错误.这是破坏的多个示例之一

I've come accoss a problem that I can't seem seem to understand why it's happening . I've got a few Fragments in my project using the support v4 library (version 21.0.3) . In these fragments I used the getContext() method for example to show Toasts . But yesterday suddently all getContext in my project broke and Android Studio shows me 26 errors while compiling . Here's one of the multiple exemples that broke

    package com.example.project.ui.fragment;


    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import com.example.project.R;
    import java.util.ArrayList;

    public class ListSizesFragment extends Fragment {

    private ArrayList<String> sizes;

    private ListView listViewSizes;
    private ArrayAdapter<String> adapter;


    public ListSizesFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list_sizes, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listViewSizes = (ListView) view.findViewById(R.id.listViewBlogs);
        sizes = new ArrayList<>(MAX_SIZES);
        sizes.addAll(getValueSizes())

        adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, sizes);
        listViewSizes.setAdapter(adapter);
        listViewSizes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String resultSize = adapter.getItem(position);
                Toast.makeText(getContext(),"result :"+resultSize,Toast.LENGHT_SHORT).show();               getActivity().setResult(Activity.RESULT_OK);
                getActivity().finish();
            }
        });
    }
}

从我在 Android 文档中看到的 (http://developer.android.com/reference/android/support/v4/app/Fragment.html#getContext()) getContext 是 support-v4 Fragment 的一种方法,所以我被困在试图找出问题所在.

From what I can see in the Android documentation (http://developer.android.com/reference/android/support/v4/app/Fragment.html#getContext()) getContext is a method of the support-v4 Fragment so I'm stuck at trying to figure out what's the problem .

在那个项目中,我也有一个 ViewPager,我的 addOnPageListener 方法没有同样的问题.

In that project I also had a ViewPager and I can't have the same problem with the addOnPageListener method .

我尝试过重建项目并使用 Gradle 文件重新同步项目,但无济于事.

I've tried Rebuilding the Project and Resyncing the project with the Gradle file but to no avail.

谢谢.

推荐答案

Fragment#getContext() 已在支持库的第 23 版中添加.如果您无意中翻转了 build.gradle 文件以使用旧版本(您说 21.0.3),则不支持 getContext().

Fragment#getContext() was added in version 23 of the support library. If you inadvertently flipped your build.gradle files to use an older version (you say 21.0.3), then getContext() is not supported.

这篇关于无法解析 android.support.v4.app.Fragment(版本 21.0.3)Android Studio 中的方法 getContext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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