>source

안드로이드로 앱을 만들고 있습니다. 조각에서 대화 상자 (timeInfoDialog)를 열려고합니다. 대화 상자는 이미 다른 활동에 대해 테스트되었으며 작동하지만 조각 활동에서 전화를 시도하면 충돌이 발생합니다.

시간 정보 대화 상자

   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
}

다음과 같은 오류가 발생합니다 OnAttach () 메소드에서 일종의 컨텍스트 오류 인 것 같습니다

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)

  • 답변 # 1

    Exception  :

    java.lang.ClassCastException: com.getodevs.jakov.pocketgourmet.AddMealStepActivity@b2d0c06implementing problem at main activity
    
    

    그것을 나타 내기 위해 던져 :

    와이즈 비즈 와이즈 비즈

    와이즈 비즈 참조  당신의

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

    여기서 onAttach 를 던지려고 했어요   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"); } }   Context 를 구현하지 않습니다 .

    timeInfoDialogListener 에서 Lisener 설정을 피하십시오  이 목적을 위해 방법을 사용하십시오 :

    AddMealStepActivity
    
    
    timeInfoDialogListener

관련 자료

  • 이전 zip - Chrome에서 한 번의 클릭으로 PC에서 다운로드 한 파일의 압축을 해제하는 방법은 무엇입니까? 맥은 왜 그렇게하지 않습니까?
  • 다음 android - 조각에서 Google지도를 사용하는 동안 작동하지 않지만 활동에서 동일한 맵이 작동하는 이유는 무엇입니까?