android - 您的活动尚未附加到应用程序实例。您不能在 onCreate 调用之前请求 ViewModel
问题描述
我尝试在其托管活动中锚定到 viewId 的 fragmentDialog 上运行测试
@Test
public void largeScreenDeviceUsesPopup() {
final FragmentUtilActivity fragmentActivity = new FragmentUtilActivity();
fragmentActivity.onCreate(null);
myDialogFragment = MyDialogFragment.create(FragmentUtilActivity.anchorId);
myDialogFragment.showNow(fragmentActivity.getSupportFragmentManager(), "");
assertThat(....)
}
private static class FragmentUtilActivity extends FragmentActivity {
public static int anchorId = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout view = new LinearLayout(this);
view.setId(anchorId);
setContentView(view);
}
}
但是我收到此错误:
Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.
我怎样才能解决这个问题?
解决方案
推荐阅读
- javascript - 为什么 SSR 比 SPA 快,反之亦然?
- svg - 如何将 SVG 过滤器应用于直线路径?
- java - 使用 selenium 时,在 Indeed 上的框架弹出窗口中,尽管切换了框架,但无法单击任何内容
- node.js - 我在 iisnode windows web 应用程序上托管了我的 Loopback 4 应用程序,从那里获取端口作为 Loopback 4 不理解的管道
- python-3.x - int() 或 dict 用于单个数字字符串到 int 的转换
- api - 如何重用 swagger 定义并删除其中的一些参数?
- javascript - 如何将点击处理程序传递给子组件
- php - 字符串值数组,而不是对象字符串数组
- sql - 在Oracle中提取子字符串
- javascript - 如何只显示一次动态创建的 html 表?