[英]Why do I get a blank screen with this code?
我正在關注Big nerd Ranch的android圖書(第二版)。 以下代碼有望顯示從CrimeLab類生成的模型數據列表(100個犯罪,每個犯罪帶有標題,日期和已解決的復選框)。在CrimeListFragment中使用recyclerView來顯示數據。 但是在構建和運行應用程序時,我得到的只是一個空白的空白屏幕。 以下是相關代碼:
public class CrimeListActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_host);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeListFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}}
CrimeListFragment.java
public class CrimeListFragment extends Fragment {
private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_crime_list,container,false);
mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI(){
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}
public class CrimeHolder extends RecyclerView.ViewHolder{
public TextView mTitleTextView;
private TextView mTitleTextView;
private TextView mDateTextView;
private CheckBox mSolvedCheckBox;
private Crime mCrime;
public CrimeHolder (View itemView){
super(itemView);
// mTitleTextView = (TextView) itemView;
mTitleTextView =(TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
mDateTextView =(TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
}
public void bindCrime(Crime crime){
mCrime = crime ;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mSolvedCheckBox.setEnabled(mCrime.isSolved());
}
}
public class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes = crimes;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.list_item_crime,parent,false);
return new CrimeHolder(view) ;
}
@Override
public void onBindViewHolder(CrimeHolder holder,int position){
Crime crime = mCrimes.get(position);
holder.bindCrime(crime);
}
@Override
public int getItemCount(){
return mCrimes.size();
}
}
以防萬一,CrimeLab.java :(提供模型數據)
public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;
public static CrimeLab get(Context context){
if (sCrimeLab==null){
sCrimeLab = new CrimeLab(context); //Hence calling the private constructor that no other class can access
}
return sCrimeLab;
}
private CrimeLab(Context context){
mCrimes = new ArrayList<>();
for (int i=0; i<100; i++){
Crime crime = new Crime();
crime.setTitle("Crime #"+i);
crime.setSolved(i%2==0);
mCrimes.add(crime);
}
}
public List<Crime> getCrimes(){
return mCrimes;
}
content_fragment_host.xml(這是由activity_fragment_host.xml編輯的,我從其中刪除了默認的AppBarLayout&Toolbar代碼,如果有幫助的話)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bignerdranch.android.crimeintent.CrimeListActivity"
tools:showIn="@layout/activity_fragment_host"
android:id="@+id/fragment_container">
</FrameLayout>
fragment_crime_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/crime_recycler_view">
</android.support.v7.widget.RecyclerView>
list_item_crime.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_solved_check_box"
android:layout_alignParentRight="true"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_title_text_view"
android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
android:textStyle="bold"
android:padding="4dp"
tools:text="Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_date_text_view"
android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
android:layout_below="@id/list_item_crime_title_text_view"
android:padding="4dp"
tools:text="Date"/>
</RelativeLayout>
這只是一個錯字。 在CrimeListFragment.java中,onCreateView更改為OnCreateView。 但是無論如何,謝謝,這讓我發瘋。
每次您使用片段並想要顯示它或替換為另一個片段時,都需要從FragmentManager調用commit()。 你的情況是fm.commit();
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeListFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
fm.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.