I am building an app in android. I am trying to open a dialog (timeInfoDialog) from a fragment. Dialog is already tested on some other activity and it is working, but when I attempt to call from the fragment activity it crashes.
time Info Dialog.
public class timeInfoDialog extends DialogFragment {
defining stuff
private timeInfoDialogListener timeListener;
private TimePickerDialog timePickerDialog;
private Button someButtons;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_add_time_info_dialog, null);
builder.setView(view).setTitle("Select time info").setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String WDO=workDayOpen.getText().toString();
String WDC=workDayClose.getText().toString();
String STO=saturdayOpen.getText().toString();
String STC=saturdayClose.getText().toString();
String SNO=sundayOpen.getText().toString();
String SNC=sundayClose.getText().toString();
timeListener.applyTimeInfo(WDO,WDC,STO,STC,SNO,SNC);
}
});
defining stuff
setOnClickListeners();
return builder.create();
}
private void setOnClickListeners(){
irrelevant
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d("ONATACH:",context.toString());
try{
timeListener = (timeInfoDialogListener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + "implementing problem at main activity");
}
}
public interface timeInfoDialogListener{
void applyTimeInfo(String WDO, String WDC, String STO, String STC, String SNO, String SNC);
}
}
LocationStepFragment
public class LocationStepFragment extends Fragment implements Step, timeInfoDialog.timeInfoDialogListener{
deffining stuff
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_step_fragment, container, false);
deffining stuff...
setOnClickListeners();
selectCustomLocationBtn.setEnabled(false);
requestLocationUpdates();
//initialize your UI
return v;
}
@Override
public VerificationError verifyStep() {
//return null if the user can go to the next step, create a new VerificationError instance otherwise
return null;
}
@Override
public void onSelected() {
//update UI when selected
}
@Override
public void onError(@NonNull VerificationError error) {
//handle error inside of the fragment, e.g. show error on EditText
}
private void setOnClickListeners() {
//calling dialog
addTimeInfoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openTimeDialog();
}
});
}
private void requestLocationUpdates(){
not relevant
}
//calling dialog method
public void openTimeDialog(){
timeInfoDialog timeInfoDialog = new timeInfoDialog();
timeInfoDialog.show(getActivity().getSupportFragmentManager(),"timeInfoDialog");
}
//dialog method
@Override
public void applyTimeInfo(String WDO, String WDC, String STO, String STC, String SNO, String SNC) {
Log.d("TimePassed",WDO + " " + WDC + " " + STO +" " + STC + " " + SNO + " " + SNC);
workdayHours = WDO + "-" + WDC;
saturdayHours = STO + "-" + STC;
sundayHours = SNO + "-" + SNC;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
not relevant
}
private boolean isServicesOK(){
not relevant
}
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
not relevant
}
I get the following error It appears to be a context error of some kind in OnAttach() method
2019-07-26 18:23:24.522 9552-9552/com.getodevs.jakov.pocketgourmet D/ONATACH::
com.getodevs.jakov.pocketgourmet.AddMealStepActivity@b2d0c06
2019-07-26 18:23:24.522 9552-9552/com.getodevs.jakov.pocketgourmet D/AndroidRuntime: Shutting down VM
2019-07-26 18:23:24.524 9552-9552/com.getodevs.jakov.pocketgourmet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.getodevs.jakov.pocketgourmet, PID: 9552
java.lang.ClassCastException: com.getodevs.jakov.pocketgourmet.AddMealStepActivity@b2d0c06implementing problem at main activity
at com.getodevs.jakov.pocketgourmet.timeInfoDialog.onAttach(timeInfoDialog.java:179)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1404)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
the Exception
:
java.lang.ClassCastException: com.getodevs.jakov.pocketgourmet.AddMealStepActivity@b2d0c06implementing problem at main activity
Thrown to indicate that :
the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0); System.out.println((String)x);
see onAttach
methode of your DialogFragmen
:
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d("ONATACH:",context.toString());
try{
timeListener = (timeInfoDialogListener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + "implementing problem at main activity");
}
}
here you tried to cast Context
to timeInfoDialogListener
but your AddMealStepActivity
does not implement timeInfoDialogListener
.
avoid setting Lisener in onAttach
and use a method for this purpose:
public void setTimeInfoDialogListener(TimeInfoDialogListener l){
this.timeListener = l;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.