我试图在AppCompatActivity中的ViewPager中的片段中创建ListView.在AppCompatActivity中,所有视图元素都是CoordinatorLayout中的包装.因为我使用了CoordinatorLayout.我必须使用RecylerView我试图遵循developer.android.com的培训 ,但我的应用程序在我的日志后停止.这是我在myFragment中停止应用程序的代码.
import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_city_list, container, false) mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list); mLayoutManager = new LinearLayoutManager(this.getActivity()); Log.d("debugMode", "The application stopped after this"); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(getNames()); mRecyclerView.setAdapter(mAdapter); return view; } //...
Mohammed Aou.. 10
用这个
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_city_list, container, false) // Replace 'android.R.id.list' with the 'id' of your RecyclerView mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list); mLayoutManager = new LinearLayoutManager(this.getActivity()); Log.d("debugMode", "The application stopped after this"); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(getNames()); mRecyclerView.setAdapter(mAdapter); return view; }
你应该在上面调用setLayoutManager
&setAdapter
methods(分别)Recyclerview
.
加,
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
你不应该使用android.R.id.list
,因为你没有使用ListFragment
.将其替换id
为您Recyclerview
(如在XML布局中).
用这个
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_city_list, container, false) // Replace 'android.R.id.list' with the 'id' of your RecyclerView mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list); mLayoutManager = new LinearLayoutManager(this.getActivity()); Log.d("debugMode", "The application stopped after this"); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new RecyclerAdapter(getNames()); mRecyclerView.setAdapter(mAdapter); return view; }
你应该在上面调用setLayoutManager
&setAdapter
methods(分别)Recyclerview
.
加,
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
你不应该使用android.R.id.list
,因为你没有使用ListFragment
.将其替换id
为您Recyclerview
(如在XML布局中).