Cannot resolve symbol error - CrimeFragment


#1

I am getting cannot resolve symbol error on CrimeFragment. What am I doing wrong? Here is my code for CrimeActivity.java:

package com.bignerdranch.android.criminalintent;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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.fragment_container);

    if (fragment == null) {
        fragment = new CrimeFragment();
        fm.beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }
}

}


#2

Could you share the debug log and the CrimeFragment.java?

It seems no error in CrimeActivity.java except a redundant “import android.support.v7.app.AppCompatActivity;”


#3

Here is CrimeFragment.java:

package com.bignerdranch.android.criminalintent;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
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 com.bignerdranch.android.criminalintent.Crime;
import com.bignerdranch.android.criminalintent.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

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 = (EditText) 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 = (Button) v.findViewById(R.id.crime_date);
    String formattedDate = new SimpleDateFormat("EEEE,MMM d,yyyy.").format(Calendar.getInstance().getTime());
    mDateButton.setText(formattedDate);
    mDateButton.setEnabled(false);

    mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Set the crime's solved property
            mCrime.setSolved(isChecked);
        }
    });

    return v;
}

}


#4

It’s so strange you add these two import clauses to your CrimeFragment.java.

Please enter Build --> Clean Project in your Android Studio toolbar and Sync Project again.


#6

I think you`re not importing your class CrimeFragment.

Adrian Gates
Sr. Developer - Apps4Rent