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);
Related
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.
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();
I have a problem regarding android layout that I am not able to resolve. I have a fragment with a layout that contains lots of elements and in particular this CardView element
<androidx.cardview.widget.CardView
android:id="#+id/search_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
app:cardBackgroundColor="#color/chartMax"
app:cardCornerRadius="20dp"
app:cardElevation="2dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:text="Cerca"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="23sp" />
</androidx.cardview.widget.CardView>
I access this element in fragment
private lateinit var searchButton: CardView
searchButton = view.search_button
and everything works fine, I can use it normally.
The problems start when I add a normal SearchView to the fragment.
<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
When I run the application it crash on this instruction
searchButton = view.search_button
This is the stacktrace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.atoka.mobile, PID: 31291
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to androidx.cardview.widget.CardView
at io.atoka.mobile.fragments.SearchFragment.getViewElements(SearchFragment.kt:57)
at io.atoka.mobile.fragments.SearchFragment.onCreateView(SearchFragment.kt:73)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
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.dispatchStateChange(FragmentManager.java:3273)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)
like if my CardView was an AppCompatImageView that is not. When I remove the SearchView the application works normally.
Anyone knows the solution? Is there any bug regarding this particular case?
Thank you
Your problem occurs because of the name you gave to your cardview
search_button is already define in default androidx.appcompat.widget.SearchView class as below:
final ImageView mSearchButton;
mSearchButton = findViewById(R.id.search_button);
so when you trying to get it from binding it's getting conflicted with the SearchView class search_button parameter
try to change the name of your cardview to something else then try,
I want to share selected text in textview with the text selection toolbar.
This is my textview.xml
<TextView
android:id="#+id/txt_content"
fontPath="fonts/Roboto-Regular.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="#{data}"
android:bufferType="spannable"
android:textColor="#color/md_black_1000"
android:textIsSelectable="true"
android:textSize="22sp" />
The textview content is a spannable with html code
binding.setData(SearchTextHighlight.highlight(searchKeyWord, Html.fromHtml(articles.getContent())));
I get the following error when i select share. I Hope my question is clear.
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:740)
at android.app.ContextImpl.startActivity(ContextImpl.java:727)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
at android.widget.TextView.shareSelectedText(TextView.java:11284)
at android.widget.TextView.onTextContextMenuItem(TextView.java:10833)
at android.widget.Editor$TextActionModeCallback.onActionItemClicked(Editor.java:3681)
at com.android.internal.policy.PhoneWindow$DecorView$ActionModeCallback2Wrapper.onActionItemClicked(PhoneWindow.java:4170)
at com.android.internal.view.FloatingActionMode$4.onMenuItemSelected(FloatingActionMode.java:108)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:801)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:155)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:954)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:944)
at com.android.internal.view.FloatingActionMode$5.onMenuItemClick(FloatingActionMode.java:136)
at com.android.internal.widget.FloatingToolbar$FloatingToolbarMainPanel$1.onClick(FloatingToolbar.java:1298)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22570)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
add this line , to the Intent you used to share
Intent i = new Intent(.........);
//add this line
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Been pulling my hair out trying to figure out why my app keeps crashing, I've nailed it down to having this in my main activities layout XML.
<com.facebook.login.widget.ProfilePictureView
android:id="#+id/pp"
android:layout_gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent">
</com.facebook.login.widget.ProfilePictureView>
Whenever I have this in my main activity layout, and I try to create any new intents (new activities, facebook sign in or requesting permissions) My app crashed with a FAILED BINDER TRANSACTION and TransactionTooLargeException.
This continues to happen when I remove the profile picture code in the activity class. It seems to be the view appearing that's casing this but I cannot think why.
This occurs not on my main testign device, but on all emulators and other devices Ive tried.
stacktrace
03-13 15:29:21.347 28962-28962/uk.co.claytapp.taggerbath E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1443592)
03-13 15:29:21.348 28962-28962/uk.co.claytapp.taggerbath D/AndroidRuntime: Shutting down VM
03-13 15:29:21.348 28962-28962/uk.co.claytapp.taggerbath E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.claytapp.taggerbath, PID: 28962
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1443592 bytes
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.os.TransactionTooLargeException: data parcel size 1443592 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:615)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3606)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3744)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
This most likely happens in the onSaveInstanceState(Bundle outState) method of the activity you are navigating away from.
To fix it, add android:saveEnabled="false" to your ProfilePictureView like so:
<com.facebook.login.widget.ProfilePictureView
android:id="#+id/profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:saveEnabled="false"
facebook:com_facebook_preset_size="normal" />
This will prevent Android to call ProfilePictureView's own onSaveInstanceState method (the view hierarchy is stored by the Activity anyway so it won't hurt).