- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个在 android java 中使用 MVC 设计模式和 fragment 的程序。我找到了我的一个 fragment 并使其正常工作,但是当我复制其他 fragment 以遵循相同的代码结构(具有专门的功能)时,我在 onCreateView 方法中遇到空指针异常。
我现在在我的垃圾笔记本电脑上,它似乎无法处理 android 仿真,所以我明天可以发布确切的错误代码。虽然我有我的源代码,但我已经用头撞墙足够长的时间来知道它在哪里坏了。
编辑:我看到了我的问题。我正在通过从每个 fragment 的 View.java 类中调用一个方法来测试我的代码。此方法更新 View 中的表。由于 View 尚未显示在屏幕上,因此尚未为它们调用 onCreateView()。由于尚未调用 onCreateView(),因此尝试访问 View 会导致空指针。有没有什么好的方法可以从我的 MainActivity 中为每个 fragment 调用 onCreateView() 以便我可以尽早初始化 View ?
(部分工作 fragment ):
public class DispatchView extends Fragment {
private final List<DispatchModel> models = new ArrayList<DispatchModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dispatchfragment, container,
false);
return view;
}
除 DispatchView 之外的所有 fragment 都会在返回 View 时中断。他们返回 null 而不是实际对象。其中一个破 fragment 段的一部分:
public class ConnectionsLogView extends Fragment {
private final List<ConnectionsLogModel> models = new ArrayList<ConnectionsLogModel>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.connectionslogfragment,
container, false);
return view;
}
fragment 被声明和初始化。在我尝试将新的数据条目(类)推送到它们之后,它们(除 Dispatch MVC 之外的任何一个)都会中断。在我的 MainActivity.java 中:
public class MainActivity extends Activity {
// Declare Tab Variables and fragment objects
private mDMI app;
ActionBar.Tab Tab1, Tab2, Tab3, Tab4;
Fragment dispatchTab = new DispatchView();
Fragment dispatchLogTab = new DispatchLogView();
Fragment activeConnectionsTab = new ConnectionsView();
Fragment connectionLogTab = new ConnectionsLogView();
DispatchModel dispatchModel;
DispatchController dispatchController;
DispatchLogModel dispatchLogModel;
DispatchLogController dispatchLogController;
ConnectionsModel connectionsModel;
ConnectionsController connectionsController;
ConnectionsLogModel connectionsLogModel;
ConnectionsLogController connectionsLogController;
public MainActivity() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (mDMI) getApplication();
dispatchModel = app.getDispatchModel();
dispatchController = new DispatchController(dispatchTab, dispatchModel);
dispatchLogModel = app.getDispatchLogModel();
dispatchLogController = new DispatchLogController(dispatchLogTab,
dispatchLogModel);
connectionsModel = app.getConnectionsModel();
connectionsController = new ConnectionsController(activeConnectionsTab,
connectionsModel);
connectionsLogModel = app.getConnLogModel();
connectionsLogController = new ConnectionsLogController(
connectionLogTab, connectionsLogModel);
setContentView(R.layout.activity_main);
xml字符串被识别在我的 R.java 中:
public static final class layout {
public static final int activity_login=0x7f030000;
public static final int activity_main=0x7f030001;
public static final int connectionsfragment=0x7f030002;
public static final int connectionslogfragment=0x7f030003;
public static final int dispatchfragment=0x7f030004;
public static final int dispatchlogfragment=0x7f030005;
}
最佳答案
不要以这种方式创建 fragment 。而是使用标准的 Android 模式:
public class FeedFragment extends Fragment {
public FeedFragment() {
super();
}
public static FeedFragment newInstance(Context context) {
if (context != null) {
sContext = context.getApplicationContext();
} else {
sContext = YourApp.getInstance().getApplicationContext();
}
return new FeedFragment();
}
}
然后不要在您的 Activity 中那样创建它们......
改为在您的 Activity 中使用标准的 Android 模式:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.content_frame);
if ( fragment == null ) {
fragment = FeedFragment.newInstance(this);
fm.beginTransaction()
.add(R.id.content_frame, dispatchTab, "DISPATCH_FRAG")
.commit();
}
}
要稍后检索 fragment ,您可以执行...
final FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag("DISPATCH_FRAG);
if (fragment != null) {
// cast and use your fragment
}
如果以后需要,您甚至可以存储引用。
关于您的 null 问题,如果没有确切的崩溃/日志,真的很难判断。
但是您的代码有点乱,这可能是原因。 fragment 生命周期很棘手。
关于java - 安卓Java : Fragments returning null view in onCreateView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086303/
我已经搜索了与此相关的每个主题,但没有一个对我有帮助...我是初学者,非常感谢您的回答...当我启动该应用程序时,一切都很好,但是当我第二次调用 HomeFragment 时,出现了问题。我试过了 F
我尝试将 Android 兼容性库(或现在称为支持库)v4 用于 pre-honeycomb android 设备。我明白,我应该使用 FragmentActivity 类,而不是使用 Fragmen
我正在创建一个选项卡式应用程序,但遇到了问题。当我单击我所在位置旁边的选项卡时,onCreateView() 运行。我的意思是,如果我有三个选项卡(设置、 map 和电机),如果我单击电机选项卡, m
我的选项卡有一个viewpager。我有三个选项卡,我在第一个选项卡中进行计算,将其存储在仍然是共享首选项的tinyDb 中。我还在第二个选项卡中进行了另一个计算,并将答案存储在tinydb中,然后通
我有一个带有 Activity 和 3 个可滑动选项卡( fragment )的应用程序。我在选项卡内创建了一些大量创建的 UI 元素,这些元素需要一些时间才能创建,第一次运行没问题,但每次我滑动它们
我不止一次看到这个问题在这里被问到,但我不知道如何解决我的问题。 我有一个用户执行此操作的应用: 1 - 打开 navigationDrawer 并选择一个选项(创建了一个 fragment )(我在
我创建了一个具有以下构造函数的自定义首选项 public CoordinatesPreference(Context context, AttributeSet attrs) { super(
我正在尝试构建一个包含多个 fragment 的应用程序,其中所有 fragment 都实现了 LoaderCallbacks。我一直在阅读 StackOverflow 上的帖子,其中大多数都指出要使
当我将 onCreateView() 中的 container 类型从 Viewgroup? 更改为 Viewgroup 时,我得到了这个错误: 这是为什么? 最佳答案 您收到错误,因为 ViewGr
我试图将 TimePicker 片段放入我创建的选项卡类中,但以下重写方法导致了问题: @Override public View onCreateView(LayoutInflater in
我遇到了一个奇怪的问题,onCreateView每次我导航到 TabLayout 中的相邻选项卡时都会被调用。这是我的代码: news_feed.java: private static TabLay
如果我有在片段的 onCreateView 中声明的变量;有没有办法可以从 onCreateView 外部访问这些变量? public View onCreateView(LayoutInflate
我当前的问题是我正在尝试触发 Animator 在两个 Activity 之间转换。我的目标是创建从一个 Activity 到另一个 Activity 的循环显示过渡。 我开始收到此错误:“无法在分离
我正在将数据从 Activity 发送到 fragment 并使用以下代码启动 fragment : if (conversation != null) { FragmentManager f
这里我写了一个简单的 Activity 和 fragment 。我改变了方向,因为它是 configuration change 调用,所以它再次调用了 Activity.onCreate() 但为什
我注意到我的一个 fragment 的 onCreateView() 从未被调用,我不知道为什么。我需要能够在 onResume() 中进行更改,但是那也从未被调用。 这是我添加 fragment 的
我正试图摆脱一个麻烦的异常。 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIn
尝试将 RecyclerView 实现到我的应用程序中,我不明白为什么我会收到“未连接适配器;跳过布局”我想我清楚地在 onCreateView 中设置了它。其他一些帖子建议将其移至 Oncreate
我想知道 onCreateView() 中的容器参数是什么,因为当我将 View 膨胀到那个容器时,我想知道这是什么 viewGroup,它是我们将添加的 Activity 的 viewGroup f
我创建了一个应用程序,它使用了 antonyt 的 InfiniteViewpager。 我想在创建 View 时在 Imageview 上添加动画,但我的动画工作了几次,但并非总是如此。这是代码:
我是一名优秀的程序员,十分优秀!