来自其他类片段的另一个类的访问函数

时间:2019-07-08 10:01:42

标签: android kotlin

我正在尝试将一个类的功能访问另一个类中的片段,但是出现以下错误

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)

下面是我第一堂课中的代码

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

下面是我在片段中上方调用的方式

 AppUtils.showError_c(context as VerifyAccountActivity,obj.getString("message"))

2 个答案:

答案 0 :(得分:0)

您需要将用于更新UI的代码放在UI线程中:

runOnUiThread(new Runnable() {
@Override
public void run() {
    //put your code here   
    }
});

答案 1 :(得分:0)

问题是我认为您正在使用该语法的应用程序上下文。在这种库中,您必须使用活动上下文的上下文。例如片段中的getActivity()和活动中的Mainactivity.this。