3

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>

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);
    }
}

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;    
    }

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

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

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

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?

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

The onCreateView method is called correctly.

Community
  • 1
  • 1
Ivan
  • 4,186
  • 5
  • 39
  • 72
  • Might be unrelated, but why are you calling `super.onSaveInstanceState` inside `onCreateView`? and this answer mentions that the container can actually be null: http://stackoverflow.com/a/13586685/1561033 – Jujuba Jul 05 '14 at 20:16
  • The onSaveInstanceState was added trying to fix this issue, if i remove that line i still have the problem. The problem is that the layout R.layout.new_reminder is not inflated in the fragment. – Ivan Jul 05 '14 at 20:25
  • When you say it is not being inflated, what error message are you getting? – Jujuba Jul 05 '14 at 20:41
  • nothing! No erro, but the view doesn't appear in the fragment. – Ivan Jul 05 '14 at 20:49
  • Would the view appear anyway, even if `container` had not been `null`, as you say it is? Wouldn't that `layout_width` set at `0dp` in the `` element prevent it to be seen on screen anyway? – Giulio Piancastelli Jul 05 '14 at 21:10

1 Answers1

1

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

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.

Jujuba
  • 451
  • 2
  • 8