Question on First Challenge,please help!


#1

Well I have finished Basic Java studies, however I have only coded very simple codes not large projects nor familiar with on how to use an API, therefore from the questions I did copy and paste some of the answers some tweaked and some on my own. Anyways, the code works perfectly fine until the screen is flipped and the application crashes when pressed on either the “DatePicker” or the “TimePicker”. Everything displays fine when in correct screen position however when flipped the application crashes. So some insight on the code would be great.

[code]TimePickerFragment
package com.bignerdranch.android.criminalintent;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment {
public static final String EXTRA_TIME = “criminalintent.TIME”;
public static final String TAG = “TimePickerFragmentE”;

Date mTime;
public static TimePickerFragment newInstance(Date date){
Bundle args = new Bundle();
args.putSerializable(EXTRA_TIME, date);
TimePickerFragment fragment = new TimePickerFragment();
fragment.setArguments(args);
return fragment;
}
private void sendResult(int resultCode) {
if(getTargetFragment() == null)
return;
Intent i = new Intent();
i.putExtra(EXTRA_TIME, mTime);
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(TAG, “onCreate”);
mTime = (Date)getArguments().getSerializable(EXTRA_TIME);

Calendar c = Calendar.getInstance();
c.setTime(mTime);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
View v = getActivity().getLayoutInflater().inflate(R.layout.time_picker, null);

TimePicker pick = (TimePicker)v.findViewById(R.id.time_picker_timePicker);
pick.setCurrentHour(hour);
pick.setCurrentMinute(minute);
pick.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
	
	public void onTimeChanged(TimePicker view, int hour, int minute) {
		Log.d(TAG, "onTimeChanged");
		Calendar c = Calendar.getInstance();
		c.setTime(mTime);
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, minute);
		mTime = c.getTime();
		getArguments().putSerializable(EXTRA_TIME, mTime);
	}
});

return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(
android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sendResult(Activity.RESULT_OK);
}
})
.create();
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub

}
[/code]

[code]CrimeFragment
package com.bignerdranch.android.criminalintent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
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.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
public static final String EXTRA_CRIME_ID = “criminalintent.CRIME_ID”;
public static final String TAG = “CrimeFragmentE”;
private static final String DIALOG_DATE = “date”;
private static final int REQUEST_DATE = 0;
private static final String TIME_PICKER = “time”;
private static final int REQUEST_TIME = 1;

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

public static CrimeFragment newInstance(UUID crimeId) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_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(EXTRA_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}


public void updateDate() {
   Log.d(TAG, "updateDate called");
   SimpleDateFormat datFormat = new SimpleDateFormat("MMM dd, yyyy");
   mDateButton.setText(datFormat.format(mCrime.getDate()));
  
}
public void updateTime(){
	Log.d(TAG, "updateTime called");
	SimpleDateFormat timFormat = new SimpleDateFormat("kk:mm:ss zzz");
	mTimeButton.setText(timFormat.format(mCrime.getDate()));
}


@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.setText(mCrime.getTitle());
    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 one too
        }
    });
    
    mDateButton = (Button)v.findViewById(R.id.crime_date);
    updateDate();
    mDateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {                	            	
            FragmentManager fm = getActivity()
                    .getSupportFragmentManager();
            DatePickerFragment dialog = DatePickerFragment
                .newInstance(mCrime.getDate());
            dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
            dialog.show(fm, DIALOG_DATE);              	
        }
    });
    mTimeButton = (Button)v.findViewById(R.id.crime_time);
    updateTime();
    mTimeButton.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			FragmentManager fm = getActivity().getSupportFragmentManager();
			TimePickerFragment time = TimePickerFragment.newInstance(mCrime.getDate());
			time.setTargetFragment(CrimeFragment.this, REQUEST_TIME);
			time.show(fm, TIME_PICKER);
			
		}
	});
    
    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set the crime's solved property
            mCrime.setSolved(isChecked);
        }
    });


    
    return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode != Activity.RESULT_OK) return;
   if (requestCode == REQUEST_DATE) {
      Date date = (Date) data
            .getSerializableExtra(DatePickerFragment.EXTRA_DATE);
      mCrime.setDate(date);
      updateDate();
   } else if (requestCode == REQUEST_TIME) {
      Date date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
      mCrime.setDate(date);
      updateTime();
   }
}

}
[/code]


#2

Hey mydreamisgenetist, check this post:

http://forums.bignerdranch.com/viewtopic.php?f=409&t=7110&hilit=TimePickerFragment

That solved my problem


#3

Well thank your for your interest, the new added feature does look great however the app still crashes when I press on the datepicker or timepicker while its flipped around. Well with new added features from the solution given by you, the app crashes the moment i press on the “time” button or the date. It is strange that only adding a resume button would create that kind of a bug.
I am guessing it must have to do something while the data is sent from the original fragment to the flipped version however being a noob, expressing such fixation into reality is not an easy task.
Reading the “fragment” section in the android API section to see if I will have a better luck, while waiting for others aid.
Thank You!

Okay so I added the Dialog Date, and TimePicker layouts in layout-land section. Well that did not work. :frowning:


#4

Try this:

  • In res\layout-land, delete your Dialog Date and TimePicker layouts
  • In fragment_crime from res\layout-land add your time button:

... <Button android:id="@+id/crime_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> ...

And whatever happens, take a look to the outputs in the Logcat.


#5

[quote=“incubusj”]Try this:

  • In res\layout-land, delete your Dialog Date and TimePicker layouts
  • In fragment_crime from res\layout-land add your time button:

... <Button android:id="@+id/crime_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> ...

And whatever happens, take a look to the outputs in the Logcat.[/quote]
yup that was it, i am kind of embarrassed haha
well
Thank You, up to the next question :slight_smile: