Problem with starting the crime activity in the criminalintent app from android programming the big nerd ranch guide


#1

so everything worked great with the criminalintent app up until page 209. "the problem with direct retrieval." i have previous experience with java but i am completely new to android programming and i am pretty much just trying to follow the directions exactly. i have gone over my code again and again but i do not understand the problem and as far as i can see my code matches the code in the book exactly. when the application is launched it runs at first but since switching from direct retrieval to using fragment arguments any click causes the entire application to crash. like i said before switching to fragment arguments clicking a list item succesfully launched the crimeActivity. i will include some of the code here. please let me know if you can find any thing wrong. thanks!

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

@Override
public void onResume(){
    super.onResume();
    updateUI();
}

private void updateUI(){
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    List<Crime> 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){
        Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
        startActivity(intent);
    }//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<CrimeHolder>{

    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

public class CrimeActivity extends SingleFragmentActivity {

private static final String EXTRA_CRIME_ID = "com.bignerdranch.android.Criminalintent.crime_id";

public static Intent newIntent(Context packageContext, UUID crimeId){
    Intent intent = new Intent(packageContext, CrimeActivity.class);
    intent.putExtra(EXTRA_CRIME_ID, crimeId);
    return intent;
}

@Override
protected Fragment createFragment(){
    UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
    return CrimeFragment.newInstance(crimeId);
}

}//end crime activity

public class CrimeFragment extends Fragment{

public static final String ARG_CRIME_ID = "crime_id";

private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;

public static CrimeFragment newInstance(UUID crimeId){
    Bundle args = new Bundle();
    args.putSerializable(ARG_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);

}//end onCreate()


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_crime, container, false);

    mTitleField = (EditText) v.findViewById(R.id.crime_title);
    mTitleField.setText(mCrime.getTitle());
    mTitleField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mCrime.setTitle(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });//end textChangedListener

    mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mCrime.setSolved(isChecked);
        }
    });//end checkedchangedlistener

    mDateButton = (Button) v.findViewById(R.id.crime_date);
    mDateButton.setText(mCrime.getDate().toString());
    mDateButton.setEnabled(false);

    return v;
}//end onCreateView()

}//end class


#2

I had a similar problem.

Make sure .CrimeActivity is included in your AndroidManifest.xml file:

<activity android:name=".CrimeActivity">
</activity>