Criminal Intent - Errors in CrimeFragment.java


#1

Hello Big Nerd Ranch Forum,

**EDIT: I want to preface this by saying that I am having problems reading the code snippets in the book because of the way that they are done in parts but the rest of the code isn’t shown. A part of the code is shown where the new code goes and the … covers where other code is between what is shown and the new code. I think I’m having a hard time adjusting to that because when something doesn’t work it’s tough for me to go back and compare with only the snippets. That being said, please see below. Please and thank you."

I am new to Android programming but I am coming along. I have pasted the code below that I have typed in for my Criminal Intent project in Big Nerd Ranch Android Programming Third addition. I get errors in red for the last block and some of my closing brackets. I have been following the book step by step and I completed GeoQuiz ok. I am not sure what is going on but I cannot proceed to run my app without fixing this. Is anyone able to look at the below code and find the reason it’s giving me errors? I have been over and over it and I am missing something. Also, is there any where that I can see the full completed CrimeFragment.java file to compare it myself?

I really appreciate your help.

ErrolNiel

See Code Below:

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
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;

import static android.widget.CompoundButton.*;

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 container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime, container, false);

    mTitleField = v.findViewById(R.id.crime_title);
    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) {

        }

        mDateButton = 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 CompoundButton.OnCheckedChangeListener()

        {
            @Override
            public void onCheckedChanged (CompoundButton buttonView,boolean isChecked){
            mCrime.setSolved(isChecked);



            }

        });




            return v


    }
}

#2

Hello, @ErrolNiel ,

I think,
the problem is that you do not close the brackets for addTextChangedListener(). You will put the closed bracket before that mDateButton = v.findViewById(R.id.crime_date).

 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) {

        }
});//here

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

#3

Thank you, @lscodex, I will try that. I like the book but I’m finding it a little hard to follow in the code examples because of the way it breaks things up. I’m sure, like everything else, it just takes some getting used to.


#4

Of course, you are right. The book is good, worth it.


#5

Thanks again, sir. Made it past that and still grinding away.