如何使用 webview 和 ksoapcall 计时器为 Android 中的 UI 线程设置当前 Activity

How to Set Current Activity for UI thread in Android using webview and ksoapcall Timer

当 运行 宁 Android 计时器时,我得到一个 E/Error: java.lang.NullPointerException

如何在 UI 线程上将当前 activity 设置为 运行?

完整的错误消息:

E/Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
E/AndroidRuntime: FATAL EXCEPTION: Timer-0

这是我的代码,请参阅下面的评论以了解异常发生的位置:

            //set a new Timer

            timer = new Timer();
            initializeTimerTask(webView);

            //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
            timer.schedule(timerTask, 10000, 10000); //
        }
    });

    return root;
}

public void initializeTimerTask(final WebView wv) {

// HERE IS WHERE THE NULL REFERENCE ERROR OCCURS (on context):

    final Activity context = this.context;

    timerTask = new TimerTask() {
        @Override
        public void run() {
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    wv.reload();
                    new SoapCall().execute();
                }
            });
        }
    };
}

您可以随时将此调用到 运行 UI 线程上的内容:

(new Handler(Looper.getMainLooper())).post(new Runnable() {
    @Override
    public void run() {

    }
});