3

在我的应用程序中,我使用的是 tabview。在其中一个选项卡中,我实现了一个刷新功能,例如:

private final Runnable r = new Runnable() {
    public void run() {
        mVar = true;
        while (mVar == true){
            calculateResult();
            //refresh();

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
};
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    handler.removeCallbacks(r);
    setContentView(R.layout.result_view);
    refresh();
    setBackGround();
}
public void refresh() {
    new Thread(r).start();
    //handler.postDelayed(r, 1000);
}

我已经尝试使用handler.removeCallbacks(r);以阻止处理程序自我重复,或者我相信什么。但是当我启动它时系统就崩溃了。

这是我的错误日志:

04-23 01:14:20.234: D/dalvikvm(14753): GC_EXTERNAL_ALLOC freed 53K, 47% free 2868K/5379K, external 0K/0K, paused 37ms
04-23 01:14:20.265: D/ATRecorder(14753): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@405178e8
04-23 01:14:20.275: D/WindowManagerImpl(14753): addView, new view, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@40523840
04-23 01:14:23.368: D/dalvikvm(14753): GC_EXTERNAL_ALLOC freed 17K, 47% free 2901K/5379K, external 1151K/1663K, paused 23ms
04-23 01:14:23.518: D/dalvikvm(14753): GC_EXTERNAL_ALLOC freed 86K, 45% free 2960K/5379K, external 2320K/2875K, paused 24ms
04-23 01:14:23.678: D/dalvikvm(14753): GC_EXTERNAL_ALLOC freed 3K, 45% free 2961K/5379K, external 8073K/10082K, paused 25ms
04-23 01:14:23.798: D/ATRecorder(14753): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@4053b728
04-23 01:14:23.878: D/dalvikvm(14753): GC_EXTERNAL_ALLOC freed 10K, 45% free 2969K/5379K, external 13845K/15875K, paused 28ms
04-23 01:14:23.898: D/ATRecorder(14753): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@40531240
04-23 01:14:23.908: D/WindowManagerImpl(14753): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@40512848
04-23 01:14:24.108: W/dalvikvm(14753): threadid=10: thread exiting with uncaught exception (group=0x4001d5a0)
04-23 01:14:24.108: E/AndroidRuntime(14753): FATAL EXCEPTION: Thread-11
04-23 01:14:24.108: E/AndroidRuntime(14753): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.ViewRoot.checkThread(ViewRoot.java:3165)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.ViewRoot.requestLayout(ViewRoot.java:677)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.view.View.requestLayout(View.java:8507)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.widget.ImageView.setImageDrawable(ImageView.java:330)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at android.widget.ImageView.setImageBitmap(ImageView.java:344)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at com.FYP.indoorGPS.TouchImageView.setImageBitmap(TouchImageView.java:153)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at com.FYP.indoorGPS.MapActivity.drawMap(MapActivity.java:163)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at com.FYP.indoorGPS.MapActivity$1.run(MapActivity.java:31)
04-23 01:14:24.108: E/AndroidRuntime(14753):    at java.lang.Thread.run(Thread.java:1027)
04-23 01:14:24.379: D/WindowManagerImpl(14753): finishRemoveViewLocked, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@40523840
04-23 01:14:24.419: D/WindowManagerImpl(14753): finishRemoveViewLocked, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@40512848
04-23 01:14:26.030: D/Process(14753): killProcess, pid=14753

谁能告诉我该怎么办?

4

2 回答 2

1

我猜问题是你的 Runnable "r" 即使在removeCallbacks(). removeCallbacks()不会停止正在运行的 Runnable,它只会阻止它启动。

此外,您refresh()再次调用“r”,因此它将被无限期地调用。因此,如果removeCallbacks()在“r”运行时调用,则没有停止“r”的代码,它将永远循环。

一个解决方案是在你的 Runnable 中有一个循环并Thread.sleep()在每个循环结束时调用。然后只需结束循环即可停止 Runnable。

例如,在您的 Runnable 中:

public void run() {
    mVar = true;
    while (mVar == true){
        //do stuff
        Thread.sleep(2000);
    }

}

让 mVar 是一个静态变量,您可以将其设置为 false 以结束循环。

另外,我建议不要在 UI 线程上运行此 Runnable,因为它可能会导致您的应用程序在该Thread.sleep(2000)方法期间停止响应。您可以使用以下内容:

new Thread(r).start();

编辑:如果您要在 Runnable 中处理视图,则处理视图的代码必须从 UI 线程运行。因此,任何从可运行文件更改视图的代码都需要通过处理程序调用。

于 2012-04-22T15:45:16.033 回答
0

我认为使用 Handler 是更好的解决方案,而不是 Thread。请参见下面的代码,它应该可以工作。我假设 mVar 是一个受控变量,将设置为 false 以停止处理程序。

private final Runnable r = new Runnable() {
    public void run() {
        if (mVar) {
            calculateResult();
            handler.postDelayed(this, 2000);
        }
    }
};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    handler.removeCallbacks(r);
    setContentView(R.layout.result_view);
    refresh();
    setBackGround();
}

public void refresh() {
    handler.postDelayed(r, 1000);
}
于 2013-05-24T09:50:14.867 回答