gpt4 book ai didi

java - 安卓Java : Fragments returning null view in onCreateView()

转载 作者:行者123 更新时间:2023-11-30 03:16:10 25 4
gpt4 key购买 nike

我目前正在开发一个在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com