Problem using DecorView with shared element transitions - android

I want to use the background of an Activity as a part of SharedElementTransition, but when adding DecorView to the transition, the app crashes with the following trace:
java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.ViewGroup
at android.app.ActivityTransitionCoordinator.setSharedElementMatrices(ActivityTransitionCoordinator.java:571)
at android.app.ActivityTransitionCoordinator.moveSharedElementsToOverlay(ActivityTransitionCoordinator.java:858)
at android.app.EnterTransitionCoordinator.viewsReady(EnterTransitionCoordinator.java:144)
at android.app.EnterTransitionCoordinator.lambda$triggerViewsReady$0(EnterTransitionCoordinator.java:164)
at android.app.-$$Lambda$EnterTransitionCoordinator$wYWFlx9zS3bxJYkN44Bpwx_EKis.run(Unknown Source:4)
at com.android.internal.view.OneShotPreDrawListener.onPreDraw(OneShotPreDrawListener.java:78)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2478)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7187)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:951)
The main reason for the crash is that the parent of DecorView when called by view.getParent() is not a ViewGroup. Is there another way to animate the background of an Activity when entering it from another Activity as a part of SharedElementTransition?
It's worth mentioning that I'm using changeImageTransform in windowSharedElementEnterTransition right now.

Related

Adding Bottom Sheet in Recycler view onclick listener

I have a Recyclerview and some items in it like imageviews and textviews.
I need to open a bottom sheet using model class, when I click the imageview in the recyclerview.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.storeeval, PID: 21795
java.lang.IllegalStateException: Fragment BottomSheetDialog{6a325f6} (605326f6-5f11-4029-9b55-15c136b1aa75) has not been attached yet.
at androidx.fragment.app.Fragment.getChildFragmentManager(Fragment.java:1075)
at com.app.storeeval.store_elements_adapter$ElementsViewHolder$1.onClick(store_elements_adapter.java:86)
at android.view.View.performClick(View.java:6616)
at android.view.View.performClickInternal(View.java:6593)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25929)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I/Process: Sending signal. PID: 21795 SIG: 9
I have tried some methods to resolve these errors from here but I couldn't succeed.
please help me in clearing this error.

Android SurfaceView gets disconnecting from surface, reason connectToSurface(reconnect) using Compose's AndoridView

I am playing around with jetpack compose and surface view and I often land into this issue where in the surfaceview gets randomly disconnected from surface. I've added the stack trace for reference. Can someone help me to fix this?
java.lang.IllegalArgumentException: detached
at MobilePlayerViewModel$createSurfaceViewOnAttachStateChangeListener$1.onViewDetachedFromWindow(MobilePlayerViewModel.kt:356)
at android.view.View.dispatchDetachedFromWindow(View.java:21362)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3949)
at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:5785)
at androidx.compose.ui.viewinterop.AndroidViewHolder.setView$ui_release(AndroidViewHolder.android.kt:95)
at androidx.compose.ui.viewinterop.AndroidViewHolder$layoutNode$1$4.invoke(AndroidViewHolder.android.kt:310)
at androidx.compose.ui.viewinterop.AndroidViewHolder$layoutNode$1$4.invoke(AndroidViewHolder.android.kt:307)
at androidx.compose.ui.node.LayoutNode.detach$ui_release(LayoutNode.kt:420)
at androidx.compose.ui.node.LayoutNode.detach$ui_release(LayoutNode.kt:432)
at androidx.compose.ui.node.LayoutNode.detach$ui_release(LayoutNode.kt:432)
at androidx.compose.ui.node.LayoutNode.detach$ui_release(LayoutNode.kt:432)
at androidx.compose.ui.node.LayoutNode.detach$ui_release(LayoutNode.kt:432)
at androidx.compose.ui.node.LayoutNode.onChildRemoved(LayoutNode.kt:314)
at androidx.compose.ui.node.LayoutNode.removeAt$ui_release(LayoutNode.kt:288)
at androidx.compose.ui.node.UiApplier.remove(UiApplier.android.kt:35)
at androidx.compose.runtime.ComposerImpl$realizeMovement$1.invoke(Composer.kt:3835)
at androidx.compose.runtime.ComposerImpl$realizeMovement$1.invoke(Composer.kt:3835)
at androidx.compose.runtime.CompositionImpl.applyChangesInLocked(Composition.kt:808)
at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:839)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:995)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:519)
at androidx.compose.runtime.Recomposer$HotReloadable.clearContent(Recomposer.kt:385)
at androidx.compose.runtime.Recomposer$RecomposerInfoImpl.saveStateAndDisposeForHotReload(Recomposer.kt:369)
at androidx.compose.runtime.Recomposer$Companion.saveStateAndDisposeForHotReload$runtime_release(Recomposer.kt:1285)
at androidx.compose.runtime.HotReloader$Companion.saveStateAndDispose(Composition.kt:1136)
at dalvik.system.VMDebug.nativeAttachAgent(Native Method)
at dalvik.system.VMDebug.attachAgent(VMDebug.java:656)
at android.app.ActivityThread.attemptAttachAgent(ActivityThread.java:4220)
at android.app.ActivityThread.handleAttachAgent(ActivityThread.java:4230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2296)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Cannot add the same observer with different lifecycles, one activity multiple fragment architecture

I'm using one activity multiple fragments architecture with bottom navigation.
On pressing the same button in bottomNav i encounter with this error.
My viewModel code in fragement:
DashboardViewModel dashboardViewModel = new ViewModelProvider(requireActivity()).get(DashboardViewModel.class);
dashboardViewModel.getLatLong().observe(getViewLifecycleOwner(),item ->{
Log.i("uni","location expense "+item);
});
and the error in logcat as follows :
2022-01-14 14:13:35.388 5033-5824/smartsense.co.in.sensephone E/OneSignal: Error updating the user record because of the null user id
2022-01-14 14:15:21.045 5033-5033/smartsense.co.in.sensephone E/AndroidRuntime: FATAL EXCEPTION: main
Process: smartsense.co.in.sensephone, PID: 5033
java.lang.IllegalArgumentException: Cannot add the same observer with different lifecycles
at androidx.lifecycle.LiveData.observe(LiveData.java:199)
at smartsense.co.in.sensephone.ui3.ExpenseFragment.onCreateView(ExpenseFragment.java:126)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7664)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Unable to get button background color

I am trying to obtain the current background color of a button from a randomized color palette. However, my app is always crashing when I try the following code.
int activeColor = ((ColorDrawable)color1.getBackground()).getColor();
I always get this error message.
Process: com.boredgiant.chora, PID: 17418
java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable
at com.boredgiant.chora.DrawActivity$103.onClick(DrawActivity.java:850)
at android.view.View.performClick(View.java:6314)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24793)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6543)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
You need to cast it to RippleDrawable
int activeColor = ((RippleDrawable)color1.getBackground()).getColorStateList().getDefaultColor();

Accessing parent view of any view in android

I have a TextInputEditText inside a TextInputLayout in my xml code.
<android.support.design.widget.TextInputLayout
android:id="#+id/textfieldContainer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:errorTextAppearance="#style/customErrorTextColor"
app:hintTextAppearance="#style/customHintTextAppearance"
app:layout_constraintTop_toBottomOf="#id/textfieldContainer">
<android.support.design.widget.TextInputEditText
android:id="#+id/bottomtextfield"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="add bottom text"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</android.support.design.widget.TextInputLayout>
Problem
I am trying to get TextInputLayout via TextInputEditText as shown below
TextInputLayout parentInputLayout = (TextInputLayout) textfield.getParent();
but this line of code throws java.lang.IllegalStateException
Question
What am I doing wrong here and how can I get parent TextInputLayout of any TextInputEditText through TextInputEditText's reference.
Stacktrace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yousafkhan.memegeneratorapp, PID: 10425
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout
at com.example.yousafkhan.memegeneratorapp.MainActivity.textFieldEmpty(MainActivity.java:64)
at com.example.yousafkhan.memegeneratorapp.MainActivity.createMeme(MainActivity.java:53)
Getting a reference to the TextInputLayout that contains the TextInputEditText can be done as the following:
TextInputEditText textfield = findViewById(R.id.bottomtextfield);
ViewGroup containingLayout = (ViewGroup) textfield.getParent();
TextInputLayout textInputLayout = (TextInputLayout) containingLayout.getParent();
This is because TextInputLayout has a FrameLayout on it that contains the TextInputEditText associated.
The view hierarchy in xml may not be same as the view herarchy returned by android. So getParent() may not return you the expected view.
Therefore you should prefer using :
TextInputLayout parentInputLayout = findViewById(R.id.textfieldContainer2);

Categories

Resources