为什么我得到一个"无法解决方法" getApplication错误?

时间:2016-04-19 08:04:47

标签: java android android-fragments syntax-error

我相信我的所有代码都是按顺序排列的,但是我已经为这个错误烦恼了好几个小时了。我试图使用片段设置一个基本的应用程序框架。如果我将项目构建为基于活动并将字符串数据与下面的代码块一起放在我的MainActivity中,我可以使代码工作,但是当我尝试将它们移动到片段时,这对我没有帮助。

在MainActivity中(相同的代码块在不使用片段的情况下工作),它被置于onCreate中,但如果我将它放在Fragment中并且我认为将它放在onCreate中并不是正确的位置就没有区别。 / p>

我是Android开发的新手,如果你能用代码示例解释你的答案,那将会有很大的帮助。

感谢您抽出宝贵时间来寻找。

我的整个片段如下。

public class BasicsPageFragment extends Fragment  {

RecyclerView recyclerView;
private Recycler_View_Adapter adapter;

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

@Override
public void onCreate(Bundle savedInstanceState) {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this

    View rootView = inflater.inflate(R.layout.recycler_view, container, false);

    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.fragment_container);
    rv.setHasFixedSize(true);


    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);

    return rootView;
}

@Override
protected void onViewCreated(Bundle savedInstanceState) {

    // below I am receiving an error (cannot resolve method 'getApplication()'
    // the line of code above it is also throwing "getView() in Fragment cannot be applied"
    // and savedInstanceState says "OnViewCreated (android.view.View, Bundle)
    // in Fragment cannot be applied to (android.os.Bundle)"
    super.onViewCreated(savedInstanceState);

    List<CardviewData> data = fill_with_data();
    recyclerView = (RecyclerView) getView(R.id.fragment_container);
    adapter = new Recycler_View_Adapter(data, getApplication());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //  errors stop here

}

//Create a list of CardviewData objects
public List<CardviewData> fill_with_data() {

    List<CardviewData> data = new ArrayList<>();

    data.add(new CardviewData("Batman vs Superman", "Following the destruction of Metropolis, Batman embarks on a personal vendetta against Superman ", R.drawable.ic_action_movie));
    data.add(new CardviewData("X-Men: Apocalypse", "X-Men: Apocalypse is an upcoming American superhero film based on the X-Men characters that appear in Marvel Comics ", R.drawable.ic_action_movie));
    data.add(new CardviewData("Captain America: Civil War", "A feud between Captain America and Iron Man leaves the Avengers in turmoil.  ", R.drawable.ic_action_movie));
    data.add(new CardviewData("Kung Fu Panda 3", "After reuniting with his long-lost father, Po  must train a village of pandas", R.drawable.ic_action_movie));
    data.add(new CardviewData("Warcraft", "Fleeing their dying home to colonize another, fearsome orc warriors invade the peaceful realm of Azeroth. ", R.drawable.ic_action_movie));
    data.add(new CardviewData("Alice in Wonderland", "Alice in Wonderland: Through the Looking Glass ", R.drawable.ic_action_movie));

    return data;
}
}

如果您需要适配器......

public class Recycler_View_Adapter extends RecyclerView.Adapter<CardviewViewHolder> {

List<CardviewData> list = Collections.emptyList();
Context context;

public Recycler_View_Adapter(List<CardviewData> list, Context context) {
    this.list = list;
    this.context = context;
}

@Override
public CardviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);
    CardviewViewHolder holder = new CardviewViewHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(CardviewViewHolder holder, int position) {
    holder.title.setText(list.get(position).title);
    holder.description.setText(list.get(position).description);
    holder.imageView.setImageResource(list.get(position).imageId);


}

@Override
public int getItemCount() {
    return list.size();
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

// Insert a new item to the RecyclerView
public void insert(int position, CardviewData data) {
    list.add(position, data);
    notifyItemInserted(position);
}
// Remove a RecyclerView item containing the CardviewData object
public void remove(CardviewData data) {
    int position = list.indexOf(data);
    list.remove(position);
    notifyItemRemoved(position);
}
}

3 个答案:

答案 0 :(得分:0)

您必须在片段中使用getActivity()而不是getApplicationContext()。它会工作!!

答案 1 :(得分:0)

使用getApplicationContext()

来自文档

  

getApplicaitonContext()返回单个全局的上下文   当前进程的应用程序对象,这通常只应在需要生命周期与当前上下文分离的上下文时使用,该上下文与进程的生命周期而不是当前组件相关联。

答案 2 :(得分:0)

更改此行

adapter = new Recycler_View_Adapter(data, getApplication());

到这个

adapter = new Recycler_View_Adapter(data, getActivity());