我尝试每 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/