java - 运行处理程序时出现问题?

标签 java android multithreading debugging handler

我尝试每 1 分钟调用一次 takepicture() 方法。因此,我尝试使用处理程序类,然后尝试在其运行函数中调用我的方法。但是,当我尝试进行逐步调试时,它根本不会进入运行方法。谁能建议我做错了什么?我正在尝试从我的 fragment 中调用它。

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
        final Handler handler = new Handler();

        final Runnable r = new Runnable() {
            public void run() {
                Log.d("HandlerThread","This is from the HandlerThread");
            takePicture();
            handler.postDelayed(this, 60000);
            }
        };


    }

最佳答案

试试这个:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            Log.d("HandlerThread","This is from the HandlerThread");
        takePicture();
        }
    }, 60000);
}

而不是在 run 方法中定义 handler.postDelayed。我只是在主线程本身中更改了调用。

如果它解决了您的问题,请标记为答案。

关于java - 运行处理程序时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48091516/

相关文章:

java - REST Web 服务始终使用 CodenameOne 客户端返回 html

java - Android 上的 NFC 不起作用

android - 如何显示图钉屏幕

java - 仅在异步任务完成后才需要运行任务

java - 为什么当我按下退出键时框架没有关闭?

android - View Pager + ImageView +Pinch Zoom + Rotation

android - 如果您在一个 Activity 中,按下 Activity 主页按钮时会调用哪些生命周期方法?

java - 用于读取 .txt 的多线程 BufferedReader

java - 查找线程池内 Activity 线程的名称: ExecutorService in java

java - Java内存泄漏?用信号量实现哲学家就餐