我相信我所有的代碼都是按順序排列的,但是我現在已經爲這個錯誤困擾了幾個小時。我試圖設置一個基本的應用程序框架使用片段。如果我將項目構造爲基於活動並將字符串數據與下面的代碼塊一起放入MainActivity中,但我不能幫助我,因爲我試圖將它們移動到片段中。爲什麼我在getApplication上出現「無法解決方法」錯誤?
在MainActivity(其中相同的代碼塊無需使用片段),它被放置在onCreate中,但是如果我將它放在片段中並沒有區別,並且我讀取將它放在onCreate中不是合適的位置。
作爲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);
}
}
使用getApplicationContext()而不是getApplication()@The綠 –