我目前正在开发一个在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;
}
答案 0 :(得分:1)
不要以这种方式创建片段。而是使用标准的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();
}
}
然后不要在你的活动中创建它们......
在您的活动中使用标准的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();
}
}
要稍后检索片段,您可以执行...
final FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag("DISPATCH_FRAG);
if (fragment != null) {
// cast and use your fragment
}
如果以后需要,您甚至可以存储参考。
关于你的null问题,如果没有确切的崩溃/日志,真的很难说。
但是你的代码有点混乱,这可能是原因。 Fragment LifeCycle很棘手。
答案 1 :(得分:0)
public void update(final BlockingDeque<Dispatch> dispatches) {
View view = getView();
// do not update if the view is null (device is not displaying that
// fragment)
if (view != null) {
TableLayout t = (TableLayout) view.findViewById(R.id.dispatchTable);
t.removeAllViews();