1

I am trying to use the Android compatibility package to create a backwards-compatible app that uses Fragments. However, it crashes when I run it on an Android v2.2 emulator. It does not crash on my Xoom (v3.2). I suspect the fragment tag in the main.xml might be the cause:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <fragment android:name="com.companyname.appname.MainMenuFragment"
        android:id="@+id/mainMenu"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent" />
</LinearLayout>

Here is the FragmentActivity:

package com.companyname.appname;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class AppName extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

And here is the Fragment:

package com.companyname.appname;

import android.support.v4.app.Fragment;

public class MainMenuFragment extends Fragment {

}

Any ideas?

Thanks

EDIT: I have targeted API level 8 (Android v2.2)

Ken
  • 2,918
  • 1
  • 25
  • 31
  • Post the stacktrace from logcat – smith324 Aug 03 '11 at 21:57
  • Post the stack trace if you want us to help you with a crash. – LeffelMania Aug 03 '11 at 21:57
  • Thanks, smith324 and LeffelMania. The error logcat showed this error: _08-03 22:03:22.946: ERROR/AndroidRuntime(938): Caused by: java.lang.IllegalStateException: Fragment com.companyname.appname.MainMenuFragment did not create a view._ So I overrode `onCreateView()` in my MainMenuFragment class and had it return a View, and this worked. Strange that it didn't crash in v3.2. – Ken Aug 03 '11 at 23:06
  • This error happened to me when I put a fragment block inside of my layout file, but didn't add it via FragmentTransaction. To solve it, I removed the fragment block from the layout file, and used a FragmentTransaction when necessary. – IgorGanapolsky May 30 '13 at 16:00

2 Answers2

2

Thanks, smith324 and LeffelMania. The error logcat showed this error: 08-03 22:03:22.946: ERROR/AndroidRuntime(938): Caused by: java.lang.IllegalStateException: Fragment com.companyname.appname.MainMenuFragment did not create a view. So I overrode onCreateView() in my MainMenuFragment class and had it return a View, and this worked. Strange that it didn't crash in v3.2.

Ken
  • 2,918
  • 1
  • 25
  • 31
1

Sometimes you doesn't want an UI to be attached to your Fragment. For example in my app I have a fragment responsible for a menu item used as an action view in the action bar. In this case you can't implement onCreateView().

As described in the Android Fragment user guide in the "Adding a fragment without a UI" section, you have to add the fragment to his activity programmatically.

Here is the code I use in my activity :

// Add the address bar fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(addressBarFragment,"address_bar_fragment");
fragmentTransaction.commit();

Note 1: I use getSupportFragmentManager() instead of getFragmentManager() because I use the compatibility library.
Note 2: new Fragment() is not called in my example because I use Roboguice for dependences injection.

a.b.d
  • 2,190
  • 3
  • 26
  • 26
  • Thanks, but my Fragment should have a UI. I was just trying to write a bare-minimum implementation to see if it worked. – Ken Apr 12 '12 at 21:11