如何解决只有创建视图层次结构的原始线程才能触及其视图?

问题描述 投票:0回答:1

我构建我的custum android应用程序。我需要关闭dilog与textView单击,但我有一些问题:我得到一个值python web应用程序托管在pythonanywhere whith javascript到我的Android活动然后旁边的android对话框,但当我点击txtclosee我有这个错误:

@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
public class WebviewPREDICTActivity extends Activity {

    Dialog myDialog;

    WebView mWebViewDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_gallery);

        myDialog = new Dialog(this);

        mWebViewDemo = (WebView) findViewById(R.id.wwebvieww);
        ButtonClickJavascriptInterface myJavaScriptInterface = new ButtonClickJavascriptInterface(WebviewPREDICTActivity.this);

        mWebViewDemo.addJavascriptInterface(myJavaScriptInterface, "MyFunction");
        mWebViewDemo.getSettings().setJavaScriptEnabled(true);
        mWebViewDemo.loadUrl("http://brahmiamine.pythonanywhere.com/predict");



    }

    public class ButtonClickJavascriptInterface {
        Context mContext;
        ButtonClickJavascriptInterface(Context c) {
            mContext = c;

        }
       //
        @JavascriptInterface
        public void onButtonClick(String TextInsideLi) {

            TextView txt5,txtclosee;
            //Button btnFollow;

            myDialog.setContentView(R.layout.show_profil);

            txt5=(TextView) myDialog.findViewById(R.id.message);
            txtclosee=(TextView) myDialog.findViewById(R.id.txtclosee);


         //   Toast.makeText(mContext, TextInsideLi, Toast.LENGTH_SHORT).show();
           // Log.i("myTag", TextInsideLi);
            //System.out.printf(String.valueOf(TextInsideLi));

            txt5.setText(TextInsideLi);

            txtclosee.setOnClickListener(new View.OnClickListener() {



                        @Override
                        public void onClick(View v) {

                    myDialog.dismiss();

                }
            });

            myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            myDialog.show();
        }
    }

}

我得到一个值python web应用程序托管在pythonanywhere whith javascript到我的Android活动然后旁边的android对话框,但当我点击txtclosee我有这个错误:

2019-04-18 17:34:35.229 18526-18526/com.androiddeft.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.androiddeft.navigationdrawer, PID: 18526
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8525)
        at android.view.ViewRootImpl.doDie(ViewRootImpl.java:7332)
        at android.view.ViewRootImpl.die(ViewRootImpl.java:7317)
        at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:497)
        at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:435)
        at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:131)
        at android.app.Dialog.dismissDialog(Dialog.java:462)
        at android.app.Dialog.-android_app_Dialog-mthref-0(Dialog.java:156)
        at android.app.-$Lambda$c44uHH2WE4sJvw5tZZB6gRzEaHI$1.$m$0(Unknown Source:4)
        at android.app.-$Lambda$c44uHH2WE4sJvw5tZZB6gRzEaHI$1.run(Unknown Source:0)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
android
1个回答
0
投票

你已经使用后台/工作线程来更新/访问UI元素,唯一的主线程可以做或者是异常,使用这个,java代码

 runOnUiThread(new Runnable() {

    @Override
    public void run() {

        // Stuff that updates the UI

    }
});
© www.soinside.com 2019 - 2024. All rights reserved.