-1

I'm trying to access function of a class to my fragment in another class, but got this following error

07-08 15:22:34.286 13344-13952/com.img.gosuperleauge E/AndroidRuntime: FATAL EXCEPTION: Thread-37
Process: com.img.gosuperleauge, PID: 13344
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6915)
    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1077)
    at android.view.View.requestLayout(View.java:19686)
    at android.view.ViewGroup.addView(ViewGroup.java:4295)
    at android.view.ViewGroup.addView(ViewGroup.java:4237)
    at android.view.ViewGroup.addView(ViewGroup.java:4210)
    at com.andrognito.flashbar.FlashbarContainerView.show$flashbar_release(FlashbarContainerView.kt:141)
    at com.andrognito.flashbar.Flashbar.show(Flashbar.kt:31)
    at com.img.gosuperleauge.Extras.AppUtils$Companion.showError(AppUtils.kt:167)
    at com.img.gosuperleauge.Fragment.PanValidationFragment$VerifyPanDetails$strRequest$1.parseNetworkError(PanValidationFragment.kt:581)
    at com.android.volley.NetworkDispatcher.parseAndDeliverNetworkError(NetworkDispatcher.java:171)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:159)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

below is the code in my first class

Flashbar.Builder(context)
                .gravity(Flashbar.Gravity.TOP)
                .duration(5000)
                .title("Error!!")
                .message(mesg)
                .icon(R.drawable.logo)
                .backgroundDrawable(R.drawable.error_bg)
                .titleColorRes(R.color.white)
                .messageColorRes(R.color.white)
                .enterAnimation(FlashAnim.with(context)
                        .animateBar()
                        .duration(750)
                        .alpha()
                        .overshoot())
                .exitAnimation(FlashAnim.with(context)
                        .animateBar()
                        .duration(400)
                        .accelerateDecelerate())
                .build()

below is how i am calling above in my fragment

 AppUtils.showError_c(context as VerifyAccountActivity,obj.getString("message"))
Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29

2 Answers2

0

You need to put your code that updates the UI in the UI thread:

runOnUiThread(new Runnable() {
@Override
public void run() {
    //put your code here   
    }
});
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
0

The problem is that I think you are using the application context in that syntax. In this kind of library, you have to use the context of the activity's context. like getActivity() in fragment and Mainactivity.this in the activity.

raj kavadia
  • 926
  • 1
  • 10
  • 30