Jav*_*Ali 1 android views android-layout android-fragments android-recyclerview
尝试使用片段内的recyclerview实现卡片列表.每次我运行它时都会在尝试打开片段时崩溃.我正在按照这个教程https://developer.android.com/training/material/lists-cards.html#Dependencies以及这个http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-和cardview上,机器人- CMS-23465
Logcat崩溃的地方.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
circles_tab.xml片段
<android.support.v7.widget.RecyclerView
android:id="@+id/circleList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"/>
Run Code Online (Sandbox Code Playgroud)
CirclesFragment
public class CirclesFragment extends Fragment {
public ArrayList<String> circles = new ArrayList<String>();
public CharSequence[] circlesList;
public String[] circlesArray = new String[circles.size()];
private List<Person> persons;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ButtonFloat FAB;
Dialog dialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.circles_tab,container,false);
// Create Cards list
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.circleList);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
initializeData();
mAdapter = new RVAdapter(persons);
mRecyclerView.setAdapter(mAdapter);
return v;
}
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Peter Parker", R.drawable.logo));
persons.add(new Person("Miles Morales",R.drawable.logo));
persons.add(new Person("Oliver Queen", R.drawable.logo));
}
Run Code Online (Sandbox Code Playgroud)
关于这个主题有许多类似的问题,但大多数问题尚未得到解决.任何帮助将不胜感激.
感谢大家
更改
mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.circleList);
Run Code Online (Sandbox Code Playgroud)
至
mRecyclerView = (RecyclerView)v.findViewById(R.id.circleList);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2165 次 |
最近记录: |