无法解析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
问题描述
我来遇到一个问题,我似乎无法理解为什么会这样.使用支持v4库(版本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();
}
});
}
}
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.
谢谢.