Android的片段:在onCreateView容器变量为空 [英] Android fragments: container variable in onCreateView is null

查看:235
本文介绍了Android的片段:在onCreateView容器变量为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于只有片段的应用程序,而实际上(我只是创造基本内容)我只有一个片段,宣布直接进入布局:

i have an application that is only fragment based, and actually (i'm just creating basic content) i have only one fragment, declared directly into the layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:name="com.italialinux.fragments.MainFragment"
        android:id="@+id/main_screen_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        />

</FrameLayout>

和该布局被载入主要活动里面,有如下code:

And this layout is loaded inside main activity, with the following code:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
    }
}

而MainFragment类是以下内容:

And the MainFragment class is the following:

public class MainFragment extends Fragment {


    final static private long ONE_SECOND = 1000;
    final static private long TWENTY_SECONDS = ONE_SECOND * 20;

    private final static String TAG = "MainFragment";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        View view = inflater.inflate(R.layout.new_reminder, container, false);
        return view;    
    }

而new_reminder布局包含短短两年的EditText和几个标签。

And the new_reminder layout contains just two EditText and several labels.

问题是,我不能充气的布局,因为在onCreateView通过容器变量为空。

The problem is that i cannot inflate the layout, since the container variable passed in onCreateView is null.

我看到了这么多的问题,如下所示:
<一href=\"http://stackoverflow.com/questions/16883831/android-fragment-is-given-a-null-container-in-oncreateview\">Android片段在onCreateView给出一个空的容器()

I saw many questions on SO, like the following: Android Fragment is given a null container in onCreateView()

和所有的说,我需要使用一个交易,但我不明白的是:
如果我有一个片段是不可变的,在布局,并不需要改变(在这种情况下就好了,在你有一个包含项目列表的ListFragment例子很多喜欢),我怎么可以膨胀的布局当前视图里面呢?

And all says that i need to use a transaction, but what i don't understood is: if i have a fragment that is immutable, in a layout, and doesn't need to change (like in that case, and like in many examples where you have a ListFragment that contains a list of items), how i can inflate the layout inside the current view?

使用ListFragment它的工作原理,但如果我不想使用它呢?

With ListFragment it works, but what if i don't want to use it?

该onCreateView方法正确调用。

The onCreateView method is called correctly.

推荐答案

在主布局,尝试使用的android:layout_width =match_parent而不是的android:layout_width =0dp

In your main layout, try using android:layout_width="match_parent" instead of android:layout_width="0dp".

和你不应该叫的onSaveInstanceState(savedInstanceState)的onCreate ;它是由当你的碎片正在下降,使其能够保存其状态框架调用。

And you shouldn't be calling onSaveInstanceState(savedInstanceState) in onCreate; it is called by the framework when your fragment is going down to allow it to save its state.

这篇关于Android的片段:在onCreateView容器变量为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆