How to fix show method in FragmentDialog












0















I write this code to show time in my project when I run the app stop working. However, when I delete show method program works without anything shown when I press the Button



any help please



this is the code



public class AlarmFragment extends Fragment implements 
TimePickerDialog.OnTimeSetListener{
private Button clockBtn;
private TextView time;
public AlarmFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_alarm, container, false);
time = view.findViewById(R.id.alarm_time);
clockBtn= view.findViewById(R.id.clock_btn);
clockBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DialogFragment clock= new TimeClock();
///////////show method /////////
clock.show(getActivity().getSupportFragmentManager(),"Time Picker");


}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Time:" + hourOfDay +" : "+minute);
}}


this is TimeClock Fragment



import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;

public class TimeClock extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


Edit:- so I changed the return statement above, then it works.But onTimeSet method in AlarmFragment did not executed
I did it as follow:-



Activity activity = new AlarmFragment().getActivity();

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener)activity, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));









share|improve this question

























  • What is the exception ?

    – pdegand59
    Nov 14 '18 at 17:13











  • @pdegand59 I edited my question

    – Shaima
    Nov 14 '18 at 17:21











  • instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

    – Nikos Hidalgo
    Nov 14 '18 at 17:36











  • @NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

    – Shaima
    Nov 14 '18 at 19:31
















0















I write this code to show time in my project when I run the app stop working. However, when I delete show method program works without anything shown when I press the Button



any help please



this is the code



public class AlarmFragment extends Fragment implements 
TimePickerDialog.OnTimeSetListener{
private Button clockBtn;
private TextView time;
public AlarmFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_alarm, container, false);
time = view.findViewById(R.id.alarm_time);
clockBtn= view.findViewById(R.id.clock_btn);
clockBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DialogFragment clock= new TimeClock();
///////////show method /////////
clock.show(getActivity().getSupportFragmentManager(),"Time Picker");


}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Time:" + hourOfDay +" : "+minute);
}}


this is TimeClock Fragment



import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;

public class TimeClock extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


Edit:- so I changed the return statement above, then it works.But onTimeSet method in AlarmFragment did not executed
I did it as follow:-



Activity activity = new AlarmFragment().getActivity();

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener)activity, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));









share|improve this question

























  • What is the exception ?

    – pdegand59
    Nov 14 '18 at 17:13











  • @pdegand59 I edited my question

    – Shaima
    Nov 14 '18 at 17:21











  • instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

    – Nikos Hidalgo
    Nov 14 '18 at 17:36











  • @NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

    – Shaima
    Nov 14 '18 at 19:31














0












0








0








I write this code to show time in my project when I run the app stop working. However, when I delete show method program works without anything shown when I press the Button



any help please



this is the code



public class AlarmFragment extends Fragment implements 
TimePickerDialog.OnTimeSetListener{
private Button clockBtn;
private TextView time;
public AlarmFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_alarm, container, false);
time = view.findViewById(R.id.alarm_time);
clockBtn= view.findViewById(R.id.clock_btn);
clockBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DialogFragment clock= new TimeClock();
///////////show method /////////
clock.show(getActivity().getSupportFragmentManager(),"Time Picker");


}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Time:" + hourOfDay +" : "+minute);
}}


this is TimeClock Fragment



import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;

public class TimeClock extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


Edit:- so I changed the return statement above, then it works.But onTimeSet method in AlarmFragment did not executed
I did it as follow:-



Activity activity = new AlarmFragment().getActivity();

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener)activity, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));









share|improve this question
















I write this code to show time in my project when I run the app stop working. However, when I delete show method program works without anything shown when I press the Button



any help please



this is the code



public class AlarmFragment extends Fragment implements 
TimePickerDialog.OnTimeSetListener{
private Button clockBtn;
private TextView time;
public AlarmFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_alarm, container, false);
time = view.findViewById(R.id.alarm_time);
clockBtn= view.findViewById(R.id.clock_btn);
clockBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DialogFragment clock= new TimeClock();
///////////show method /////////
clock.show(getActivity().getSupportFragmentManager(),"Time Picker");


}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Time:" + hourOfDay +" : "+minute);
}}


this is TimeClock Fragment



import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;

public class TimeClock extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


Edit:- so I changed the return statement above, then it works.But onTimeSet method in AlarmFragment did not executed
I did it as follow:-



Activity activity = new AlarmFragment().getActivity();

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener)activity, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));






java android android-fragments fragment android-dialogfragment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 22:05







Shaima

















asked Nov 14 '18 at 17:12









ShaimaShaima

53




53













  • What is the exception ?

    – pdegand59
    Nov 14 '18 at 17:13











  • @pdegand59 I edited my question

    – Shaima
    Nov 14 '18 at 17:21











  • instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

    – Nikos Hidalgo
    Nov 14 '18 at 17:36











  • @NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

    – Shaima
    Nov 14 '18 at 19:31



















  • What is the exception ?

    – pdegand59
    Nov 14 '18 at 17:13











  • @pdegand59 I edited my question

    – Shaima
    Nov 14 '18 at 17:21











  • instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

    – Nikos Hidalgo
    Nov 14 '18 at 17:36











  • @NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

    – Shaima
    Nov 14 '18 at 19:31

















What is the exception ?

– pdegand59
Nov 14 '18 at 17:13





What is the exception ?

– pdegand59
Nov 14 '18 at 17:13













@pdegand59 I edited my question

– Shaima
Nov 14 '18 at 17:21





@pdegand59 I edited my question

– Shaima
Nov 14 '18 at 17:21













instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

– Nikos Hidalgo
Nov 14 '18 at 17:36





instead of (TimePickerDialog.OnTimeSetListener) getActivity() when you return from TimeClock, you need to pass your TimePickerDialog listener

– Nikos Hidalgo
Nov 14 '18 at 17:36













@NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

– Shaima
Nov 14 '18 at 19:31





@NikosHidalgo you are right,I did that then it works, but on onTimeSet method did not work time textView didn`t change

– Shaima
Nov 14 '18 at 19:31












2 Answers
2






active

oldest

votes


















0














In the Activity that hosts your AlarmFragment, you must implement TimePickerDialog.OnTimeSetListener.



For example:



public class MainActivity implements TimePickerDialog.OnTimeSetListener {

...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(this, "chosen hour of day: " + hourOfDay, Toast.LENGTH_LONG).show();
}
}





share|improve this answer
























  • I implement it already

    – Shaima
    Nov 14 '18 at 18:59











  • You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

    – Ben P.
    Nov 14 '18 at 19:55











  • import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

    – Shaima
    Nov 14 '18 at 22:09













  • My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

    – Ben P.
    Nov 15 '18 at 2:40











  • You are right . it works Thank you

    – Shaima
    Nov 15 '18 at 8:13



















0














You're getting a ClassCastException because your getActivity is not a TimePickerDialog.OnTimeSetListener, but your AlarmFragment is:



// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));


You can refactor your TimeClock class to delegate the listener:



public class TimeClock extends DialogFragment {

private TimePickerDialog.OnTimeSetListener listener;

public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


And set your listener before TimeClock.show:



DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");





share|improve this answer


























  • I got an error (cannot resolve setOnTimeSetListener method )

    – Shaima
    Nov 14 '18 at 19:50











  • Because you haven't written it..?

    – Aaron
    Nov 14 '18 at 19:51











  • @Shaima, I've updated my answer, hope it reads better.

    – Aaron
    Nov 14 '18 at 19:53











  • I Wrote it just as you did but still have the same problem

    – Shaima
    Nov 14 '18 at 21:52











  • @Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

    – Aaron
    Nov 14 '18 at 21:58











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305499%2fhow-to-fix-show-method-in-fragmentdialog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In the Activity that hosts your AlarmFragment, you must implement TimePickerDialog.OnTimeSetListener.



For example:



public class MainActivity implements TimePickerDialog.OnTimeSetListener {

...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(this, "chosen hour of day: " + hourOfDay, Toast.LENGTH_LONG).show();
}
}





share|improve this answer
























  • I implement it already

    – Shaima
    Nov 14 '18 at 18:59











  • You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

    – Ben P.
    Nov 14 '18 at 19:55











  • import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

    – Shaima
    Nov 14 '18 at 22:09













  • My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

    – Ben P.
    Nov 15 '18 at 2:40











  • You are right . it works Thank you

    – Shaima
    Nov 15 '18 at 8:13
















0














In the Activity that hosts your AlarmFragment, you must implement TimePickerDialog.OnTimeSetListener.



For example:



public class MainActivity implements TimePickerDialog.OnTimeSetListener {

...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(this, "chosen hour of day: " + hourOfDay, Toast.LENGTH_LONG).show();
}
}





share|improve this answer
























  • I implement it already

    – Shaima
    Nov 14 '18 at 18:59











  • You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

    – Ben P.
    Nov 14 '18 at 19:55











  • import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

    – Shaima
    Nov 14 '18 at 22:09













  • My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

    – Ben P.
    Nov 15 '18 at 2:40











  • You are right . it works Thank you

    – Shaima
    Nov 15 '18 at 8:13














0












0








0







In the Activity that hosts your AlarmFragment, you must implement TimePickerDialog.OnTimeSetListener.



For example:



public class MainActivity implements TimePickerDialog.OnTimeSetListener {

...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(this, "chosen hour of day: " + hourOfDay, Toast.LENGTH_LONG).show();
}
}





share|improve this answer













In the Activity that hosts your AlarmFragment, you must implement TimePickerDialog.OnTimeSetListener.



For example:



public class MainActivity implements TimePickerDialog.OnTimeSetListener {

...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(this, "chosen hour of day: " + hourOfDay, Toast.LENGTH_LONG).show();
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 18:02









Ben P.Ben P.

24.1k32049




24.1k32049













  • I implement it already

    – Shaima
    Nov 14 '18 at 18:59











  • You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

    – Ben P.
    Nov 14 '18 at 19:55











  • import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

    – Shaima
    Nov 14 '18 at 22:09













  • My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

    – Ben P.
    Nov 15 '18 at 2:40











  • You are right . it works Thank you

    – Shaima
    Nov 15 '18 at 8:13



















  • I implement it already

    – Shaima
    Nov 14 '18 at 18:59











  • You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

    – Ben P.
    Nov 14 '18 at 19:55











  • import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

    – Shaima
    Nov 14 '18 at 22:09













  • My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

    – Ben P.
    Nov 15 '18 at 2:40











  • You are right . it works Thank you

    – Shaima
    Nov 15 '18 at 8:13

















I implement it already

– Shaima
Nov 14 '18 at 18:59





I implement it already

– Shaima
Nov 14 '18 at 18:59













You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

– Ben P.
Nov 14 '18 at 19:55





You don't. Maybe you imported the wrong class? The stacktrace is very specific: com.example.mypc.bookorganizer.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener... if you implemented it this would not happen.

– Ben P.
Nov 14 '18 at 19:55













import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

– Shaima
Nov 14 '18 at 22:09







import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; //////these are the class are imported in AlarmFragment

– Shaima
Nov 14 '18 at 22:09















My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

– Ben P.
Nov 15 '18 at 2:40





My answer says, and the stacktrace also clearly says, that your activity (com.example.mypc.bookorganizer.MainActivity) must implement the interface, not your fragment.

– Ben P.
Nov 15 '18 at 2:40













You are right . it works Thank you

– Shaima
Nov 15 '18 at 8:13





You are right . it works Thank you

– Shaima
Nov 15 '18 at 8:13













0














You're getting a ClassCastException because your getActivity is not a TimePickerDialog.OnTimeSetListener, but your AlarmFragment is:



// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));


You can refactor your TimeClock class to delegate the listener:



public class TimeClock extends DialogFragment {

private TimePickerDialog.OnTimeSetListener listener;

public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


And set your listener before TimeClock.show:



DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");





share|improve this answer


























  • I got an error (cannot resolve setOnTimeSetListener method )

    – Shaima
    Nov 14 '18 at 19:50











  • Because you haven't written it..?

    – Aaron
    Nov 14 '18 at 19:51











  • @Shaima, I've updated my answer, hope it reads better.

    – Aaron
    Nov 14 '18 at 19:53











  • I Wrote it just as you did but still have the same problem

    – Shaima
    Nov 14 '18 at 21:52











  • @Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

    – Aaron
    Nov 14 '18 at 21:58
















0














You're getting a ClassCastException because your getActivity is not a TimePickerDialog.OnTimeSetListener, but your AlarmFragment is:



// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));


You can refactor your TimeClock class to delegate the listener:



public class TimeClock extends DialogFragment {

private TimePickerDialog.OnTimeSetListener listener;

public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


And set your listener before TimeClock.show:



DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");





share|improve this answer


























  • I got an error (cannot resolve setOnTimeSetListener method )

    – Shaima
    Nov 14 '18 at 19:50











  • Because you haven't written it..?

    – Aaron
    Nov 14 '18 at 19:51











  • @Shaima, I've updated my answer, hope it reads better.

    – Aaron
    Nov 14 '18 at 19:53











  • I Wrote it just as you did but still have the same problem

    – Shaima
    Nov 14 '18 at 21:52











  • @Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

    – Aaron
    Nov 14 '18 at 21:58














0












0








0







You're getting a ClassCastException because your getActivity is not a TimePickerDialog.OnTimeSetListener, but your AlarmFragment is:



// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));


You can refactor your TimeClock class to delegate the listener:



public class TimeClock extends DialogFragment {

private TimePickerDialog.OnTimeSetListener listener;

public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


And set your listener before TimeClock.show:



DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");





share|improve this answer















You're getting a ClassCastException because your getActivity is not a TimePickerDialog.OnTimeSetListener, but your AlarmFragment is:



// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));


You can refactor your TimeClock class to delegate the listener:



public class TimeClock extends DialogFragment {

private TimePickerDialog.OnTimeSetListener listener;

public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}


And set your listener before TimeClock.show:



DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 22:31

























answered Nov 14 '18 at 19:36









AaronAaron

1,7432212




1,7432212













  • I got an error (cannot resolve setOnTimeSetListener method )

    – Shaima
    Nov 14 '18 at 19:50











  • Because you haven't written it..?

    – Aaron
    Nov 14 '18 at 19:51











  • @Shaima, I've updated my answer, hope it reads better.

    – Aaron
    Nov 14 '18 at 19:53











  • I Wrote it just as you did but still have the same problem

    – Shaima
    Nov 14 '18 at 21:52











  • @Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

    – Aaron
    Nov 14 '18 at 21:58



















  • I got an error (cannot resolve setOnTimeSetListener method )

    – Shaima
    Nov 14 '18 at 19:50











  • Because you haven't written it..?

    – Aaron
    Nov 14 '18 at 19:51











  • @Shaima, I've updated my answer, hope it reads better.

    – Aaron
    Nov 14 '18 at 19:53











  • I Wrote it just as you did but still have the same problem

    – Shaima
    Nov 14 '18 at 21:52











  • @Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

    – Aaron
    Nov 14 '18 at 21:58

















I got an error (cannot resolve setOnTimeSetListener method )

– Shaima
Nov 14 '18 at 19:50





I got an error (cannot resolve setOnTimeSetListener method )

– Shaima
Nov 14 '18 at 19:50













Because you haven't written it..?

– Aaron
Nov 14 '18 at 19:51





Because you haven't written it..?

– Aaron
Nov 14 '18 at 19:51













@Shaima, I've updated my answer, hope it reads better.

– Aaron
Nov 14 '18 at 19:53





@Shaima, I've updated my answer, hope it reads better.

– Aaron
Nov 14 '18 at 19:53













I Wrote it just as you did but still have the same problem

– Shaima
Nov 14 '18 at 21:52





I Wrote it just as you did but still have the same problem

– Shaima
Nov 14 '18 at 21:52













@Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

– Aaron
Nov 14 '18 at 21:58





@Shaima I don't know what you did, as long as your TimePickerDialog second argument is not an activity type then it should work.

– Aaron
Nov 14 '18 at 21:58


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305499%2fhow-to-fix-show-method-in-fragmentdialog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues