خطای در loadingView در اکتیویتی

پرسیده شده
فعالیت 964 روز پیش
دیده شده 417 بار
0

با سلام و وقت بخیر

مبحث lodingView را با جاوا پیاده سازی کردم. در fragment کار به درستی انجام میشه (استفاده از دستور getView) ولی در activity ها حتی با وجود اینکه از CoordinatorLayout استفاده کرده ام،  هنگام تبدیل rootView به CoordinatorLayout خطای زیر را میدهد.

 

java.lang.ClassCastException: com.android.internal.policy.DecorView cannot be cast to androidx.coordinatorlayout.widget.CoordinatorLayout

کد تبدیل بدین شرح است: (سه راه مختلف را چک کرده ام که همگی با خطا روبرو شده اند.)

 public void setProgressIndicator(boolean mustShow) {
//        CoordinatorLayout rootView= (CoordinatorLayout)getWindow().getDecorView().getRootView();
//        CoordinatorLayout rootView= (CoordinatorLayout) getWindow().getDecorView().findViewById(android.R.id.content);
        CoordinatorLayout rootView=(CoordinatorLayout) getWindow().getCurrentFocus().getRootView();

        Context contextView =this;

            if(rootView!=null){
                if(contextView!=null){
                    View loadingView=rootView.findViewById(R.id.loadingView);
                    if(loadingView==null && mustShow){
                        loadingView= LayoutInflater.from(contextView).inflate(R.layout.view_loading,rootView,false);

                        if(loadingView.getParent() != null) {
                            ((ViewGroup)loadingView.getParent()).removeView(loadingView); // <- fix
                        }
                        rootView.addView(loadingView);
                    }
                    if(loadingView!=null){
                        if(mustShow)
                            loadingView.setVisibility(View.VISIBLE);
                        else
                            loadingView.setVisibility(View.GONE);
                    }
                }
            }


    }

 

فایل activity_login.xml به شرح ذیل است. (البته یک progressbar هم به صورت دستی داده بودم که حذف نکردم هنوز)

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bkcolor">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/loginForm"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="64dp"
            android:padding="32dp">

            <ProgressBar
                android:id="@+id/loginProgressBar"
                style="?android:attr/progressBarStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:visibility="invisible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/forgetPasswordBTN" />

            <TextView
                android:id="@+id/forgetPasswordBTN"
                style="@style/LinkStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:text="@string/forgotPassword"
                android:textDirection="rtl"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rememberMeCHBOX" />

            <CheckBox
                android:id="@+id/rememberMeCHBOX"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:checked="true"
                android:text="@string/rememberMe"
                android:textColor="@color/black"
                android:textDirection="rtl"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/loginBTN" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/loginBTN"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="@string/login"
                android:textDirection="rtl"
                android:textSize="20sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/passwordTVLayout" />

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/usernameTVLayout"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/username"
                android:textDirection="rtl"
                app:boxBackgroundColor="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/usernameTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textDirection="rtl" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/passwordTVLayout"
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:hint="@string/password"
                android:textDirection="rtl"
                app:boxBackgroundColor="@color/white"
                app:endIconMode="password_toggle"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/usernameTVLayout">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/passwordTV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:textDirection="rtl" />
            </com.google.android.material.textfield.TextInputLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

 

در نظر دارم OnClickListener دکمه ‘’ورود"  تابع getUser را فراخوانی کنم.

public  void getUser() {

        loginProgressBar.setVisibility(View.VISIBLE);
        setProgressIndicator(true);
            apiService = new ApiService(this, TAG);
            try{             //if server address isn't correct retrofit , retrofitApiService don't create and apiService crash
                apiService.getUser(userName, password)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SingleObserver<RootUser>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {

                                compositeDisposable.add(d);
                            }

                            @Override
                            public void onSuccess(@NonNull RootUser rootUser) {
                                loginProgressBar.setVisibility(View.INVISIBLE);
//                                setProgressIndicator(false);
                                if(rootUser.isSuccess())
                                {
                                    // user is  first of user List
                                    if(rootUser.getUsers().size() !=0){
                                        User user =rootUser.getUsers().get(0);
                                        //ToDO change input after find ThePermition type
                                        myUserManager.saveUserInfo(
                                                user.getToken(),
                                                user.getFirstname(),
                                                user.getLastname(),
                                                userName,
                                                password,
                                                user.getName());
                                        myUserManager.setRememberInfo(rememberMeCHBOX.isChecked());
                                        goToMainActivity();
                                    }
                                }
                                else{
                                    if(rootUser.getMessages().size() !=0) {
                                        String message = rootUser.getMessages().get(0);
                                        Log.i(TAG, "OnSuccess:  error is " + message);
                                        showSnakbar(message);
                                    }
                                }
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {
                                loginProgressBar.setVisibility(View.INVISIBLE);
//                                setProgressIndicator(false);
                                Log.i(TAG, "On Exception error :  error is "+e.toString());
                                showSnakbar(getString(R.string.msgErrorConnectServer));
                            }
                        });
            }
            catch (Exception e){

                showSnakbar(getString(R.string.msgErrorServerAddress));
                loginProgressBar.setVisibility(View.INVISIBLE);
//				setProgressIndicator(false);
            }

    }

 

فعلا در debug در خط  setProgressIndicator(true)  خطایی صادر میشود که در بالا ذکر شده است.

راهکار شما برای حل این مشکل چیست؟

 

با سپاس از شما

 

 

فایل پیوست

0
حذف شده

در حال حاضر با کد زیر loadingView نمایش داده شد اما به دنبال راه حل مناسب تری هستم که اطمینان داده باشم همیشه جوابگوست.

 

 Context contextView =this;
 ViewGroup rootView=(ViewGroup )((Activity)contextView).getWindow().getDecorView().getRootView() ;
فایل پیوست

راضیه کارگر شورکی

توسط

راضیه کارگر شورکی

14 شهریور 00

حذف شده
راه حل موجود در ویدیو راه حل بهینه ای هست که میتونید استفاده کنید.
پوریا شفیعی

14 شهریور 00