有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

导致ViewRootImpl$CalledFromErrorThreadException的java计时器?

我有一种感觉,我的计时器导致了这次崩溃。奇怪的是,我似乎无法在我的模拟器中重现它,但我的游戏控制台崩溃报告告诉我,用户不断收到这种崩溃:

安卓.view.ViewRootImpl$CalledFromWrongThreadException: 
at 安卓.view.ViewRootImpl.checkThread (ViewRootImpl.java:7988)
at 安卓.view.ViewRootImpl.invalidateChildInParent (ViewRootImpl.java:1392)
at 安卓.view.ViewGroup.invalidateChild (ViewGroup.java:5426)
at 安卓.view.View.invalidateInternal (View.java:14959)
at 安卓.view.View.invalidate (View.java:14923)
at 安卓.view.View.invalidate (View.java:14907)
at 安卓.widget.TextView.checkForRelayout (TextView.java:8624)
at 安卓.widget.TextView.setText (TextView.java:5137)
at 安卓.widget.TextView.setText (TextView.java:4962)
at 安卓.widget.TextView.setText (TextView.java:4937)
at com.kjdion.smoketracker.MainActivity$1.run (MainActivity.java:97)
at java.util.TimerThread.mainLoop (Timer.java:555)
at java.util.TimerThread.run (Timer.java:505)

这是我的MainActivity代码:

package com.kjdion.smoketracker;

import 安卓.content.Intent;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.View;
import 安卓.widget.TextView;

import com.facebook.stetho.Stetho;
import com.google.安卓.gms.ads.AdRequest;
import com.google.安卓.gms.ads.AdView;
import com.google.安卓.gms.ads.MobileAds;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    long longLast;
    boolean timerStarted = false;
    TextView sinceView;

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

        MobileAds.initialize(this, "ca-app-pub-4097235499795154~8279081984");
        AdView mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        //Stetho.initializeWithDefaults(this);
        setViews();
    }

    public void startLogActivity(View view) {
        Intent intent = new Intent(this, LogActivity.class);
        startActivity(intent);
    }

    public void startChartActivity(View view) {
        Intent intent = new Intent(this, ChartActivity.class);
        startActivity(intent);
    }

    public void startHealthActivity(View view) {
        Intent intent = new Intent(this, HealthActivity.class);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        setViews();
    }

    public void setViews() {
        // init vars
        long date = Helpers.currentTime();
        String na = getResources().getString(R.string.text_na);
        String sinceLast = na;
        String longestGone = na;
        String smokedToday = na;
        String lowestDay = na;
        sinceView = findViewById(R.id.sinceLast);
        TextView longestView = findViewById(R.id.longestGone);
        TextView todayView = findViewById(R.id.smokedToday);
        TextView lowestView = findViewById(R.id.lowestDay);

        // grab db
        LogDatabaseHelper logDatabaseHelper = new LogDatabaseHelper(this);
        SQLiteDatabase database = logDatabaseHelper.getWritableDatabase();
        Cursor cursor;

        // since last
        cursor = database.rawQuery("" +
                "SELECT * " +
                "FROM " + LogDatabaseHelper.TABLE_NAME + " " +
                "ORDER BY id DESC " +
                "LIMIT 1", null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();

            longLast = (date - cursor.getLong(cursor.getColumnIndex("date"))) * 1000;
            sinceLast = Helpers.toDuration(longLast);

            if (!timerStarted) {
                timerStarted = true;
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // update since last text every second
                        sinceView.setText(Helpers.toDuration(longLast));
                        longLast = longLast + 1000;
                    }
                }, 0, 1000);
            }
        }
        cursor.close();

        // longest gone
        cursor = database.rawQuery("" +
                "SELECT * FROM " + LogDatabaseHelper.TABLE_NAME + " " +
                "ORDER BY length DESC " +
                "LIMIT 1" +
                "", null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();

            long longGone = cursor.getLong(cursor.getColumnIndex("length")) * 1000;
            if (longGone > 0) {
                longestGone = Helpers.toDuration(longGone);
            }
        }
        cursor.close();

        // smoked today
        cursor = database.rawQuery("" +
                "SELECT sum(`amount`) as `sum` " +
                "FROM " + LogDatabaseHelper.TABLE_NAME + " " +
                "WHERE date(`date`, 'unixepoch') = date("+date+", 'unixepoch')" +
                "", null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();

            if (!cursor.isNull(cursor.getColumnIndex("sum"))) {
                smokedToday = Helpers.toFraction(cursor.getDouble(cursor.getColumnIndex("sum")), 1000);
            }
        }
        cursor.close();

        // lowest day
        cursor = database.rawQuery("" +
                "SELECT sum(`amount`) as `sum` " +
                "FROM " + LogDatabaseHelper.TABLE_NAME + " " +
                "WHERE date(`date`, 'unixepoch') < date("+date+", 'unixepoch')" +
                "GROUP BY date(`date`, 'unixepoch') " +
                "ORDER BY `sum` ASC " +
                "LIMIT 1" +
                "", null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();

            if (!cursor.isNull(cursor.getColumnIndex("sum"))) {
                lowestDay = Helpers.toFraction(cursor.getDouble(cursor.getColumnIndex("sum")), 1000);
            }
        }
        cursor.close();
        database.close();

        // set view text
        sinceView.setText(sinceLast);
        longestView.setText(longestGone);
        todayView.setText(smokedToday);
        lowestView.setText(lowestDay);
    }
}

是计时器引起的吗?我必须把计时器放在UI线程上吗


共 (1) 个答案

  1. # 1 楼答案

    在工作线程(非UI线程)上执行TimerTask。必须从主(UI)线程访问UI(sinceView)。在Activity中有一个助手方法来实现这一点:runOnUiThread

    MainActivity.this.runOnUiThread(new Runnable() {
       sinceView.setText(Helpers.toDuration(longLast));
    });