I am currently working on a program that uses an MVC design pattern and fragments in android java. I have figured out one of my fragments and gotten it working but when I copied the other fragments to follow the same code structure (with specialized functionality), I get a null pointer exception in the onCreateView method.
I'm on my junk laptop right now and it can't seem to handle android emulation so I can post the exact error code tomorrow. I have my source code though and I have been hitting my head against the wall long enough to know where it is breaking.
EDIT: I see my problem. I'm testing the my code by calling a method from within the View.java class from each fragment. This method updates a table in the view. Since the views haven't been displayed on screen yet, onCreateView() hasn't been called for them. Since onCreateView() hasn't been called, trying to access the view results in a null pointer. Is there any good way to call onCreateView() for each fragment from my MainActivity just so I can initialize the views early?
(Part of the working 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;
}
All fragments, except for DispatchView, break upon returning view. They are returning null rather than an actual object. Part of one of the broken fragments:
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;
}
The fragments are declared and initialized. They (any of them except the Dispatch MVC) break after I try to push a new data entry (class) into them. In my 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);
The xml strings are identified In my 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;
}