Reputation: 195
I am trying to launch a timepicker from a button in a fragment
I've read post regarding the same problems about the Error but the codes doesn't match on what I have started thats why i posted this
TimePickerFragment.java
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int cHour= c.get(Calendar.HOUR_OF_DAY);
int cMinute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener) getActivity(), cHour, cMinute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
I use the BluetoothChat application and edit it
in the BluetoothFrament.java
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mMoistIn = (TextView) view.findViewById(R.id.moistIn);
mTempIn = (TextView) view.findViewById(R.id.tempIn);
mOutEditText = (EditText) view.findViewById(R.id.edit_text_out);
mSendButton = (Button) view.findViewById(R.id.button_send);
moistProgress = (CircleProgress)view.findViewById(R.id.moistcircle_progress);
pbTemp = (CircleProgress)view.findViewById(R.id.tempcircle_progress);
buttonGoSched = (Button)view.findViewById(R.id.gotoSched);
//tempSwitch = (Switch)view.findViewById(R.id.switch1);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void setupChat() {
Log.d(TAG, "setupChat()");
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm");
strDate = "" + mdformat.format(calendar.getTime());
// Initialize the compose field with a listener for the return key
mOutEditText.setOnEditorActionListener(mWriteListener);
// tabHost.setup();
try {
buttonGoSched.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
android.support.v4.app.DialogFragment timePicker = new TimePickerFragment(); timePicker.show(getActivity().getSupportFragmentManager(), "time picker");
}
});
}catch(Exception e){
Log.e(TAG,"error in sched: "+e);
}
// Initialize the send button with a listener that for click events
mSendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Send a message using content of the edit text widget
View view = getView();
if (null != view) {
TextView textView = (TextView) view.findViewById(R.id.edit_text_out);
String message = textView.getText().toString();
sendMessage(message);
Log.d(TAG, "Water pump duration set to: "+message);
}
}
});
In Logcat.
04-10 18:55:22.647 17937-17937/com.example.android.bluetoothplantdroid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.bluetoothplantdroid, PID: 17937
java.lang.ClassCastException: com.example.android.bluetoothplantdroid.MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
at com.example.android.bluetoothplantdroid.TimePickerFragment.onCreateDialog(TimePickerFragment.java:23)
at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:310)
at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1132)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I'm trying to change the
timePicker.show(getActivity().getSupportFragmentManager(), "time picker");
to
timePicker.show(getSupportFragmentManager(), "time picker");
Because it is the original code But it has error "Cannot Resolve Method 'getSupportFragmentManager()'"
Upvotes: 0
Views: 1159
Reputation: 309
About the
MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
it is happening because you are trying to cast your activity to OnTimeSetListener
return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener) getActivity(), cHour, cMinute, android.text.format.DateFormat.is24HourFormat(getActivity()));
Make sure that your activity is extending the OnTimeSetListener, and if it so, you may try to convert right to your activity
return new TimePickerDialog(getActivity(),(MainActivity) getActivity(), cHour, cMinute, android.text.format.DateFormat.is24HourFormat(getActivity()));
because your activity will be a instance of OnTimeSetListener.
Also, I do not recommend using the getActivity() to get the listener from your fragment, its better if you have your listener not attached to your activity.
Upvotes: 0
Reputation: 75788
MainActivity cannot be cast to android.app.TimePickerDialog$OnTimeSetListener
Which means you can not call a method which is declared in the subclass.
Problem coming form
android.support.v4.app.DialogFragment timePicker = new TimePickerFragment();
Try with
TimePickerFragment timePicker = new TimePickerFragment ();
timePicker.show(getActivity().getSupportFragmentManager(), "time picker");
"Cannot Resolve Method 'getSupportFragmentManager()'"
Make sure your ROOT Activity extends
Did you using ?
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Upvotes: 1