Android Java:onCreateView()中返回空视图的片段
我目前正在开发一个在android java中使用MVC设计模式和片段的程序。我已经找到了一个片段并使其工作,但是当我复制其他片段以遵循相同的代码结构(具有专门的功能)时,onCreateView方法中出现了空指针异常 我现在在我的垃圾笔记本电脑上,它似乎无法处理android仿真,所以我可以明天发布准确的错误代码。虽然我有我的源代码,但我已经把头撞到墙上足够长的时间了,我知道它在哪里被破坏了 编辑:我明白我的问题了。我正在测试我的代码,方法是从每个片段的View.java类中调用一个方法。此方法更新视图中的表。因为视图还没有显示在屏幕上,所以还没有为它们调用onCreateView()。由于尚未调用onCreateView(),因此尝试访问该视图将导致空指针。有没有什么好方法可以为MainActivity中的每个片段调用onCreateView(),这样我就可以提前初始化视图 (工作片段的一部分): xml字符串被标识 在我的R.java中:Android Java:onCreateView()中返回空视图的片段,java,android,nullpointerexception,fragment,oncreate,Java,Android,Nullpointerexception,Fragment,Oncreate,我目前正在开发一个在android java中使用MVC设计模式和片段的程序。我已经找到了一个片段并使其工作,但是当我复制其他片段以遵循相同的代码结构(具有专门的功能)时,onCreateView方法中出现了空指针异常 我现在在我的垃圾笔记本电脑上,它似乎无法处理android仿真,所以我可以明天发布准确的错误代码。虽然我有我的源代码,但我已经把头撞到墙上足够长的时间了,我知道它在哪里被破坏了 编辑:我明白我的问题了。我正在测试我的代码,方法是从每个片段的View.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;
}
不要以这种方式创建片段。而是使用标准的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
}
如果以后需要,甚至可以存储引用
关于空问题,如果没有确切的崩溃/日志,很难判断
但是你的代码有点凌乱,这可能是原因。片段生命周期很棘手 不要这样创建片段。而是使用标准的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,则我的更新函数不会尝试向(不存在)视图中的(不存在)表添加行和条目。我在每个视图中都实现了这一点,如下所示:
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();
public void更新(最终封锁deque调度){
View=getView();
//如果视图为空,则不更新(设备未显示该视图)
//碎片)
如果(视图!=null){
TableLayout=(TableLayout)view.findviewbyd(R.id.dispatchTable);
t、 移除所有视图();
我自己修复了它。如果视图返回null,我的更新函数不会尝试在(不存在的)视图中向(不存在的)表添加行和条目。我在每个视图中实现了如下操作:
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();
public void更新(最终封锁deque调度){
View=getView();
//如果视图为空,则不更新(设备未显示该视图)
//碎片)
如果(视图!=null){
TableLayout=(TableLayout)view.findviewbyd(R.id.dispatchTable);
t、 移除所有视图();
我很好奇,与新FragmentSubclass()
相比,第一个模式的优点是什么,除了保留一个可以在整个类中使用的sContext
实例之外?它总是返回新FragmentSubclass()
无论如何……这被认为是最佳实践,因为Android将使用默认构造函数初始化片段。现在这无关紧要,但如果你添加一个参数,你将在没有意识到@JohnnyZ所说的内容的情况下破坏它。这也是安卓开发人员认可的标准实践(或者它应该这样做)而且这将使未来的您和未来的代码维护人员更容易使用。构造函数可以是私有的(应该),除非您打算在带有某种适配器的ViewPager中使用片段,在这种情况下,您需要一个默认的公共构造函数。使用这种上下文传递的优点是您不需要(也不会)将您的片段绑定到包含活动,这样您就永远不会有错误的上下文硬引用(=内存泄漏)。我很好奇,第一种模式与newfragmentsubclass()相比有什么优势
,除了保留一个可在整个类中使用的sContext
实例之外?它总是返回newfragmentsubclass()
无论如何……这被认为是最佳实践,因为Android将使用默认构造函数初始化片段。现在这无关紧要,但如果你添加一个参数,你将在没有意识到@JohnnyZ所说的内容的情况下破坏它。这也是安卓开发人员认可的标准实践(或者它应该这样做)而且这将使未来的您和未来的代码维护人员更容易使用。构造函数可以是私有的(应该),除非您打算在带有某种适配器的ViewPager中使用片段,在这种情况下,您需要一个默认的公共构造函数。使用这种上下文传递的优点是您不需要(也不会)将您的片段绑定到包含活动,这样您就永远不会有不正确的上下文硬引用(=内存泄漏)。