Issue with Chapter 8 exercise at runtime


#1

I feel a little out to sea without a paddle. I have gone through the exercise CriminalIntent starting in chapter 7 through chapter 8 two unique times now and cannot get it to function properly. It comes up with so many errors that I don’t know what to possibly do.

Here are the code snippets and maybe someone can point in me in the right direction. Note, I slipped in the code for the second project, renamed CriminalIntense. These are the three files with the issues.

From CrimeActivity.java

package com.bignerdranch.android.com.criminalintense;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;

public class CrimeActivity extends FragmentActivity
{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crime);
		
		FragmentManager fm = getSupportFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
		
		if(fragment == null) {
			fragment = new CrimeFragment();
			fm.beginTransaction()
				.add(R.id.fragmentContainer, fragment)
				.commit();
		}
	}

}

From Crime.java

	
	public void crime() {
		mId = UUID.randomUUID();
		mDate = new Date;
	}

From CrimeActivity.java

package com.bignerdranch.android.com.criminalintense;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
	private Crime mCrime;
	private EditText mTitleField;
	private Button mDateButton;
	private CheckBox mSolvedCheckBox;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCrime = new Crime();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_crime, parent, false);
	
		mTitleField = (EditText)v.findViewById(R.id.crime_title);
		mTitleField.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(
					charSequence c, int start, int before, int count) {
				mCrime.setTitle(c.toString());
			}
			public void beforeTextChanged(charSequence c, int start, int count, int after) {
				//this space intentionally left blank
			}
			public void afterTextChanged(Editable c) {
				//This too
			}
		});
		
		mDateButton = (Button)v.findViewById(R.id.crime_date);
		mDateButton.setText(mCrime.getDate().toString());
		mDateButton.setEnabled(false);
		
		mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
		mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//Set the crimes solved property
				mCrime.setSolved(isChecked);
			}
		});
		
		return v;
	}
}

Not sure if this helps, or if there is some other way that I could get help or help myself. Any suggestions appreciated.
Thanks,
Archer


#2

Not sure how I can help you archie without seeing the error log that LogCat is producing. My intuition is saying it has something to do with how you’ve got your packages set up. Just save the log after you run the code and attach it to your reply.


#3

One thing that jumps out at me: instead of this:

   public void crime() {
      mId = UUID.randomUUID();
      mDate = new Date;
   }

You should have this:

   public Crime() {
      mId = UUID.randomUUID();
      mDate = new Date();
   }

This code is a constructor, not a method call.


#4

Another one:

         public void onTextChanged(
               charSequence c, int start, int before, int count) {
            mCrime.setTitle(c.toString());
         }

CharSequence should be capitalized here.

I think you’re actually very close. Instead of being paralyzed by all the errors, patiently go through and see if you can come up with a solution for each one. If you can’t solve on error, move on to the next one. You’ll find that your huge sea of errors is actually caused by a very small number of errors in your code.


#5

Thanks guys, I got by the issue. Much appreciated! :smiley: