view.post 没有执行 运行()

view.post not executing run()

我有一个运行良好的应用程序,但一年后没有人碰过它,我决定做一个 卷土重来,但我在 android api 24 及之后遇到 problem.running 应用程序,应用程序只是不执行 view.post() 和 运行() 在里面,在 onCreateView() 里面 .该应用程序甚至没有崩溃,它正在运行,但缺少 ui.
我已经做了一些调试,它只是跳过了一些 reason.i 曾经认为这是一个线程问题,但现在我不知道了。 这是 class:

public class CategoriesFragment extends Fragment {
final String dbname = "app";
final String tbname = "categories";
DynamicUI ui;
int rowLength = 3;
final int dbver = 1;
public CategoriesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    final ScrollView l = (ScrollView) inflater.inflate(R.layout.fragment_categories_grid, container, false);
    l.post(new Runnable() {
        @Override
        public void run() {
            ui.addUtilitiesToLayout((LinearLayout) getActivity().findViewById(R.id.moreLayout), false);
            ui.addCategoriesToLayout((LinearLayout) getActivity().findViewById(R.id.categLayout), rowLength);
            // code you want to run when view is visible for the first time
        }
    });
    ui = new DynamicUI(getActivity(),DatabaseHelper.RIGHTS);
    return inflater.inflate(R.layout.fragment_categories_grid, container, false);

  }
}

在 api 23 上,例如,代码 运行 非常完美,这两行工作正常:

ui.addUtilitiesToLayout((LinearLayout) getActivity().findViewById(R.id.moreLayout), false);
ui.addCategoriesToLayout((LinearLayout) getActivity().findViewById(R.id.categLayout), rowLength);

这就是我觉得奇怪的地方:

l.post(new Runnable() {

它只是跳过了那个。 这真的很烦人。 我真的很感谢你的帮助

问题出在这一行

return inflater.inflate(R.layout.fragment_categories_grid, container, false);

应该是

return l;

inflater.inflate returns 每次调用一个新视图。您的 onCreateView 返回的内容与您发布的内容不同