i am new to android programming but i have some prior java experience. i am working on a project from the big nerd ranch guide to android programming. because i lack experience with android i am pretty much copying the code while i learn and not trying to get creative with it. however i have reached a point in the criminal intent application where my the app crashes every time i click an item in my RecyclerView.
I have searched the web and found other problems kind of like this one but they aren’t helping me figure this out. as i have said i am new to android and extras, intents and all that. can anyone help me identify the cause of this and how to fix it? i am posting some of my code as well as the Exceptions stack trace. this is the list fragment that contains the recycler view:
package com.bignerdranch.android.criminalintent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ExpandedMenuView;
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;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.IllegalFormatCodePointException;
import java.util.List;
public class CrimeListFragment extends Fragment{
private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;
@Override
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;
}//end onCreate
private void updateUI(){
CrimeLab crimeLab = CrimeLab.get(getActivity());
List crimes = crimeLab.getCrimes();
if (mAdapter == null){
mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}else {
mAdapter.notifyDataSetChanged();
}
}//end updateUI()
private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;
private ImageView mSolvedImageView;
public CrimeHolder(LayoutInflater inflater, ViewGroup parent){
super(inflater.inflate(R.layout.list_item_crime, parent, false));
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
mSolvedImageView = (ImageView) itemView.findViewById(R.id.crime_solved);
}//end constructor
@Override
public void onClick(View view){
try {
Intent intent = CrimePagerActivity.newIntent(getActivity(), mCrime.getId());
startActivity(intent); // something wrong with the activity starting when clicked!
}catch(Exception e){
Log.d("ONCLICK", e.toString());
}
}//end onCLick()
public void bind(Crime crime){
mCrime = crime;
mTitleTextView.setText(crime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
}//end bind()
}//end CrimeHolder inner Class
private class CrimeAdapter extends RecyclerView.Adapter{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes = crimes;
}//end constructor
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new CrimeHolder(layoutInflater, parent);
}// end onCreateViewHolder
@Override
public void onBindViewHolder(CrimeHolder holder, int position){
Crime crime = mCrimes.get(position);
holder.bind(crime);
}
@Override
public int getItemCount(){
return mCrimes.size();
}
}//end CrimeAdapter inner Class
}//end CrimeListFragment class
and here is the class for the activity that is supposed to be launched on click:
package com.bignerdranch.android.criminalintent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ViewPropertyAnimatorCompatSet;
import java.util.List;
import java.util.UUID;
public class CrimePagerActivity extends AppCompatActivity {
private static final String EXTRA_CRIME_ID = "com.bignerdranch.android.criminalintent.crime_id";
private ViewPager mViewPager;
private List<Crime> mCrimes;
public static Intent newIntent(Context packageContext, UUID crimeId){
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mViewPager = (ViewPager) findViewById(R.id.crime_view_pager);
mCrimes = CrimeLab.get(this).getCrimes();
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
@Override
public Fragment getItem(int position) {
Crime crime = mCrimes.get(position);
return CrimeFragment.newInstance(crime.getId());
}
@Override
public int getCount() {
return mCrimes.size();
}
});
}// end onCreate()
}//end crimePagerAtivity class
and here is the code for the CrimeLab class:
package com.bignerdranch.android.criminalintent;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;
public static CrimeLab get(Context context){
if(sCrimeLab == null){
sCrimeLab = new CrimeLab(context);
}
return sCrimeLab;
}//end get()
private CrimeLab(Context context){
}//end constructor
public List<Crime> getCrimes(){
mCrimes = new ArrayList<>();
for (int i = 0; i < 100; i++){
Crime crime = new Crime();
crime.setTitle("Crime #" + i);
crime.setSolved(i % 2 == 0); //every other crime will be checked!
mCrimes.add(crime);
}
return mCrimes;
}//end getCrimes
public Crime getCrime(UUID id){
for (Crime crime : mCrimes){
if (crime.getId().equals(id)){
return crime;
}
}
return null;
}//end getCrime()
}//end CrimeLab class
and here is my Exeptions stack trace that is logged from my try/catch block in CrimeHolders onClick():