为什么在我传递参数时不调用第二个方法?

Why is the second method not called while I pass a parameter?

这是我以前没有遇到过的奇怪的事情。我在 TestsViewModel.class 中有处理程序。我需要像 startMapThreads() 方法 (startMapThreads(viewModel.handler)) 中的参数一样设置它。当我在没有处理程序的情况下调用它时,一切正常。但是,如果我将处理程序设置为参数,则无法使用第二种方法。我也不做任何更改只添加参数。有谁知道为什么我不能连同参数一起到达第二种方法?

在第一张图片上,我使用了 3 方法,在第二张图片中,我使用了处理程序参数,只有第一种方法。

public class TestsViewModel extends ViewModel {

    public MutableLiveData<DataClass> testResult = new MutableLiveData<>();
    public Handler handler;

    public TestsViewModel() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Log.i("tester", "result: " + (DataClass) msg.obj);
            }
        };
    }
}

public class FragmentMaps extends Fragment {

    private static FragmentMaps mapsInstance;
    private static FragmentMapsBinding binding;
    public TestsViewModel viewModel;

    public static FragmentMaps getMapsInstance() {
        if (mapsInstance == null) {
            mapsInstance = new FragmentMaps();
        }
        return mapsInstance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMapsBinding.inflate(inflater, container, false);

        initObjects();
   
        return binding.getRoot();
    }

    private void initObjects() {
        viewModel = new ViewModelProvider(this,
                new TestModelFactory())
                .get(TestsViewModel.class);
    }

    public void startMapThreads() {
        Log.i("tester", "method called 1");
        ThreadsManager threadsManager = new ThreadsManager();
        threadsManager.startMapThreads();
        //displayProgressBars();
    }
}
public class ThreadsManager {

    public static boolean stopThreads = false; //We change this variable MainActivity
    public static long timeOfStartTest = 0;

    public void startMapThreads() {
        Log.i("tester", "method called 2");
        createMapThreadWithMap(() -> new TreeMap());
        createMapThreadWithMap(() -> new HashMap());
    }

    private void createMapThreadWithMap(MapFactory mapFactory) {
        Log.i("tester", "method called 3");
        stopThreads = false;
        timeOfStartTest = new Date().getTime();

        MapRunnable mapRunnable = new MapRunnable();

        new Thread(mapRunnable.addingNew(mapFactory.create())).start();
        new Thread(mapRunnable.searchByKey(mapFactory.create())).start();
        new Thread(mapRunnable.removing(mapFactory.create())).start();
    }
}

我终于找到了这一切的原因。我在 viewModel 中创建了处理程序,并在构造函数中对其进行了初始化。但是我没有初始化looper。因此,当我尝试获取方法 2 时,它处于另一个循环中。处理程序构造函数重复,您需要这样初始化:

Handler handler = new Handler(Looper.getMainLooper());

重复:

Handler handler = new Handler();