【发布时间】:2013-12-03 21:02:33
【问题描述】:
我目前正在开发一个在 android java 中使用 MVC 设计模式和片段的程序。我已经找到了我的一个片段并让它工作,但是当我复制其他片段以遵循相同的代码结构(具有专门的功能)时,我在 onCreateView 方法中得到一个空指针异常。
我现在在我的垃圾笔记本电脑上,它似乎无法处理 android 仿真,所以我明天可以发布确切的错误代码。不过我有我的源代码,而且我已经用头撞墙了足够长的时间,知道它在哪里坏了。
编辑:我看到了我的问题。我正在通过从每个片段的 View.java 类中调用一个方法来测试我的代码。此方法更新视图中的表。由于视图尚未显示在屏幕上,因此尚未为它们调用 onCreateView()。由于尚未调用 onCreateView(),因此尝试访问视图会导致空指针。有什么好方法可以为我的 MainActivity 中的每个片段调用 onCreateView() 以便我可以及早初始化视图?
(部分工作片段):
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,在返回视图时中断。它们返回 null 而不是实际对象。 破碎碎片之一的一部分:
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;
}
片段被声明和初始化。在我尝试将新的数据条目(类)推送到它们之后,它们(除了 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;
}
【问题讨论】:
标签: java android nullpointerexception fragment oncreate