メモ55|CalledFromWrongThreadExceptionエラーの処理

14263 ワード

エラーメッセージ


サブスレッドで直接UIを更新すると、CalledFromWrongThreadExceptionエラーが表示されます.エラーは次のとおりです.
12-20 15:39:04.730: E/AndroidRuntime(2763):     FATAL EXCEPTION: Timer-0
12-20 15:39:04.730: E/AndroidRuntime(2763):     android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4775)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:833)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.requestLayout(View.java:15499)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.requestLayout(View.java:15499)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.requestLayout(View.java:15499)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.requestLayout(View.java:15499)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.requestLayout(View.java:15499)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.setFlags(View.java:8450)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at android.view.View.setVisibility(View.java:5720)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at com.evan.http.MainActivity.NETData(MainActivity.java:41)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at com.evan.http.MainActivity.access$0(MainActivity.java:29)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at com.evan.http.MainActivity$5.run(MainActivity.java:72)
12-20 15:39:04.730: E/AndroidRuntime(2763):     at java.util.Timer$TimerImpl.run(Timer.java:284)

なぜこの問題が発生したのか、ここにはなぜサブスレッドでUIを更新できないのかについての議論がある.prepare()このサブスレッドがToastできない問題は同じToastがなぜサブスレッド内で実行できないのかということです

ソリューション

   1.  Activity.runOnUiThread(Runnable);
   2.  Handler(sendMessage(Message) or post(Runnable));
   3.  AsyncTask ;

私が使っているのは、このRunnableオブジェクトをrunOnUIThread(Runnable)に渡すことです.Runnableイメージをuiプログラムで呼び出すことができます
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myClickHandler();
        initTime();
        initView();
    }

    private void NETData() {
        tv_net.setVisibility(View.GONE);
        if (net) {
            runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.i("md", "ture");
            }
        });
        new Thread(){
        }.start();
        }else{
            tv_net.setVisibility(View.VISIBLE);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i("md", "false");
                }
            });
            new Thread(){
            }.start();
        }
    }

    TextView tv_net;
    private void initView() {
        tv_net=(TextView) findViewById(R.id.tv_netinfo);
    }

    Timer timer;
    TimerTask timerTask;
    private void initTime() {
        if (timer==null) {
            timer = new Timer();
        }
        if (timerTask!=null) {
            timerTask.cancel();
        }
        timerTask = new TimerTask() {

            @Override
            public void run() {
                myClickHandler();
                NETData();
            }
        };
        timer.schedule(timerTask,50,5000);
    }

    public void myClickHandler() {
        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    networking(timei);
                    timei=(int) SystemClock.uptimeMillis();
                    net=true;
                }
            });
            new Thread(){
            }.start();
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Netend(timei);
                    net=false;
                }
            });
            new Thread(){
            }.start();
        }
    }

    int timei;
    boolean net;
    private void networking(int i) {
            if (((int) SystemClock.uptimeMillis()-timei)>5050) {
                Toast.makeText(MainActivity.this, " !", 0).show();
            }
    }
    private void Netend(int i) {
                Toast.makeText(MainActivity.this, " !", 0).show();
    }
}

私のAndroid征途


転載先:https://www.cnblogs.com/xiangevan/p/10751662.html