NullPointerException at SetContentView

2,960 views
Skip to first unread message

Burger Szilard

unread,
Jul 24, 2012, 3:09:10 AM7/24/12
to robol...@googlegroups.com
Hi.

I am new to Android and Robolectric. I have made a small app and would like to test it but i get the following error:

java.lang.RuntimeException: error inflating layout/activity_main
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:372)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:106)
at android.app.Activity.setContentView(Activity.java)
at com.swisscom.wingo.activities.ActivityMain.onCreate(ActivityMain.java:25)
at view.FragmentEpgInfoScreenTest.setUp(FragmentEpgInfoScreenTest.java:27)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:280)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NullPointerException
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:54)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.loadClass(ViewLoader.java:261)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.loadFragmentClass(ViewLoader.java:274)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.constructFragment(ViewLoader.java:192)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.create(ViewLoader.java:174)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:150)
at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:153)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:372)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99)
at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99)
at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
at android.app.Activity.setContentView(Activity.java)
at com.swisscom.wingo.activities.ActivityMain.onCreate(ActivityMain.java:25)
at view.FragmentEpgInfoScreenTest.setUp(FragmentEpgInfoScreenTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:280)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

My Test Class looks like this:

@RunWith(RobolectricTestRunner.class)
public class FragmentEpgInfoScreenTest {

    private ActivityMain activity;
    private ImageView logo;

    @Before
    public void setUp() throws Exception {
        activity = new ActivityMain();
        activity.onCreate(null);
    }

    @Test
    public void shouldHaveALogo() throws Exception {

        assertNotNull(activity);

        TextView channelNumber = (TextView) activity.findViewById(R.id.fragment_epg_info_screen_text_view_channel);

        assertNotNull(channelNumber);

        ShadowTextView shadowChannelNumber = Robolectric.shadowOf(channelNumber);

        assertEquals(shadowChannelNumber.getText(), "08");
    }

}

On create:
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

My application is a really simple one: the main activity displays a video and i use fragments to display some informations on top if the video.

Would appreciate any help i could get.
Thanks

Burger Szilard

unread,
Jul 24, 2012, 3:15:52 AM7/24/12
to robol...@googlegroups.com
I am using: 
android 4.0.3
junit 4.10
robolectric-1.2-20120719.143712-109-jar-with-dependencies.jar

Aaron VonderHaar

unread,
Jul 24, 2012, 7:53:57 PM7/24/12
to robol...@googlegroups.com

Fragment support is still quite incomplete, especially when inflating fragments from xml.  I'd suggest either taking a stab at debugging the problem, or changing your app to instantiate your fragments in code rather than using the fragment tag in xml (we've done quite a bit more testing of robolectric with this style of fragments use).

Maybe someone using fragments in a current project can comment further?

--Aaron V.

Burger Szilard

unread,
Jul 25, 2012, 3:47:05 AM7/25/12
to robol...@googlegroups.com
I have found out that the fragments where the problem. I have found 2 issues that caused the problem:
1. Using android.app.Fragment : this cause the problem because Robolectric used android.support.v4.app.Fragment. Know that this was done because older versions need to be supported also, but ion my app i dont need the support library (yet) and dont want to use it because my target is android 4. Dont need support for older versions. 
Would be cool if robolectric would search for the used classes from the code and so it can be more dinamic. If the code uses the nativ classes, robolectric should use them also. 

2. This line:
<fragment
        android:id="@+id/fragment_video_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="myproject.fragments.FragmentVideoPlayer" />
If i removed this element , problem did not occure. This should also be fixed, because i dont think that a good development is that i change a good way of implementation just because of tests. 

Robolectric should support this too. 

Valtteri Virtanen

unread,
Jul 25, 2012, 3:57:02 AM7/25/12
to robol...@googlegroups.com
Hello,

Maybe you can implement your suggestions? Just clone Robolectrics project and after you are done just create pull request to Github? Robolectric-project can be found here: https://github.com/pivotal/robolectric 

- Valtteri

Johannes

unread,
Jul 25, 2012, 11:56:30 AM7/25/12
to robol...@googlegroups.com
So, I have a similar problem.

I have two custom fragments in my xml file. I did a lot of debugging today and found out that in the TestFragmentManager the method startFragment cannot get a view. It tries to get a container but fails and onCreateView with a container==null doesn't seem to work.

public void startFragment(Fragment fragment) {
        ViewGroup container = null;
        ShadowFragment shadowFragment = shadowOf(fragment);
        if (shadowOf(activity).getContentView() != null) {
            container = (ViewGroup) activity.findViewById(shadowFragment.getContainerViewId());
        }

        View view = fragment.onCreateView(activity.getLayoutInflater(), container, shadowFragment.getSavedInstanceState());
        shadowFragment.setView(view);

        fragment.onViewCreated(view, null);
        if (container != null) {
            if (shadowFragment.getShouldReplace()) {
                container.removeAllViews();
            }
            if (view != null) {
                container.addView(view);
            }
        }

        fragment.onActivityCreated(shadowFragment.getSavedInstanceState());
        fragment.onStart();
    }

Johannes

Johannes

unread,
Jul 27, 2012, 7:10:42 AM7/27/12
to robol...@googlegroups.com
I would implement the solution if somebody would give me a guidance on this. I son't have that much experience with robolectric.

Aaron VonderHaar

unread,
Jul 27, 2012, 12:12:33 PM7/27/12
to robol...@googlegroups.com

It's been a while since I worked on in, but I believe when inflating fragments from xml, there will be no container view at the point of code you indicated. Instead, the inflation code (ViewLoader?) attaches the fragment's view to the view hierarchy after the fragment has been initialized.

The lifecycle for fragment inflation is somewhat incorrect as it currently stands: real Android creates the fragments in an uninitialized state during inflation and runs the on* lifecycle methods when the corresponding methods of the FragmentActivity are called. In contrast, robolectric currently calls many of the lifecycle methods *during* inflation.  I don't know if this is related to what you are seeing, but if it is, TestFragmentManager will need to become a bit more sophisticated to behave more exactly like Android.

--Aaron V.

Su Wang

unread,
Aug 9, 2012, 2:48:17 PM8/9/12
to robol...@googlegroups.com
I have exactly the same issue as Burger, we are using android.app.Fragment but Robolectric is using android.support.v4.app.Fragment.
I have tried to write a class Fragment and bind it in my test runner, but it seems doesn't work(I will still get the same error), is this a practical way? Or is there anything wrong in my code?
Can anyone help to take a look? Thanks a lot!

See how I bind it in the runner:
@Override
public void beforeTest(Method method) {
  Robolectric.bindShadowClass(ShadowFragment.class);
}

See my own shadow class:

package com.xxx.xxx;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.view.View;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

@Implements(Fragment.class)
public class ShadowFragment {
  protected View view;
  protected Activity activity;
  private String tag;
  private Bundle savedInstanceState;
  private int containerViewId;
  private boolean shouldReplace;

  public void setView(View view) {
      this.view = view;
  }

  public void setActivity(Activity activity) {
      this.activity = activity;
  }

  @Implementation
  public View getView() {
    return view;
  }

  @Implementation
  public Activity getActivity() {
    return activity;
  }

  @Implementation
  public void startActivity(Intent intent) {
    activity.startActivity(intent);
  }

  @Implementation
  public void startActivityForResult(Intent intent, int requestCode) {
    activity.startActivityForResult(intent, requestCode);
  }

  @Implementation
  final public FragmentManager getFragmentManager() {
    return activity.getFragmentManager();
  }

  @Implementation
  public String getTag() {
    return tag;
  }

  @Implementation
  public Resources getResources() {
    return getActivity().getResources();
  }

  public void setTag(String tag) {
    this.tag = tag;
  }

  public void setSavedInstanceState(Bundle savedInstanceState) {
    this.savedInstanceState = savedInstanceState;
  }

  public Bundle getSavedInstanceState() {
    return savedInstanceState;
  }

  public void setContainerViewId(int containerViewId) {
    this.containerViewId = containerViewId;
  }

  public int getContainerViewId() {
    return containerViewId;
  }

  public void setShouldReplace(boolean shouldReplace) {
    this.shouldReplace = shouldReplace;
  }

  public boolean getShouldReplace() {
    return shouldReplace;
  }
}
Message has been deleted

Tim Baverstock

unread,
Aug 13, 2012, 9:26:53 AM8/13/12
to robol...@googlegroups.com
I find that if my Fragment class implements onCreateView() with something that inflates a view using the supplied inflater, and returns it, then it will successfully become part of an inflated view:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Must implement this BEFORE trying to inflate a layout with it in Robolectric!
        return inflater.inflate(R.layout.time, container, false);
    }

Unfortunately for ListFragment I have to inflate a view too, even though its default onCreateView is plain java code in the static library that I should be able to rely upon.

Aaron VonderHaar

unread,
Aug 15, 2012, 12:45:50 AM8/15/12
to robol...@googlegroups.com
Hi Su, I believe the binding of additional shadows must happen in the
bindShadowClasses method of your custom runner.

See the example here: https://gist.github.com/2306799
--
--Aaron V.
[ http://github.com/avh4 ]

Su Wang

unread,
Aug 15, 2012, 1:37:02 PM8/15/12
to robol...@googlegroups.com
Hi Aaron,
Thanks for your reply, I have tried this, but it seems doesn't work. I will get the same error:

java.lang.RuntimeException: error inflating layout/activity_gmm
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
	at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:372)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:102)
	at android.app.Activity.setContentView(Activity.java)
Caused by: java.lang.NullPointerException
	at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:54
)
	at com.xtremelabs.robolectric.res.ViewLoader.loadClass(ViewLoader.java:261)
	at com.xtremelabs.robolectric.res.ViewLoader.loadFragmentClass(ViewLoader.java:274)
	at com.xtremelabs.robolectric.res.ViewLoader.constructFragment(ViewLoader.java:192)
	at com.xtremelabs.robolectric.res.ViewLoader.create(ViewLoader.java:174)
	at com.xtremelabs.robolectric.res.ViewLoader.inflate(ViewLoader.java:150)
	at com.xtremelabs.robolectric.res.ViewLoader.inflate(ViewLoader.java:153)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
	at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:372)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:616)
	at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99)
	at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:102)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:616)
	at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99)
	at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
	at android.app.Activity.setContentView(Activity.java)

I can see below warnings in the log:
Warning: an error occurred while binding shadow class: ShadowGeoPoint
Warning: an error occurred while binding shadow class: ShadowItemizedOverlay
Warning: an error occurred while binding shadow class: ShadowMapController
Warning: an error occurred while binding shadow class: ShadowMapActivity
Warning: an error occurred while binding shadow class: ShadowMapView
Warning: an error occurred while binding shadow class: ShadowOverlayItem
WARNING: Unable to find path to Android SDK

Does my issue relate to those warnings?
Thanks a lot!

Su Wang

unread,
Aug 15, 2012, 2:22:33 PM8/15/12
to robol...@googlegroups.com
Sorry, the warning "WARNING: Unable to find path to Android SDK" is solved by setting the correct Android SDK path.
And I can see another error log below:
Exception in thread "Thread-1" java.lang.RuntimeException: java.io.FileNotFoundException: /.../workspace/75f9c8e32bf7255ed0bca1d210550685/2f2babb5-87c5-408b-85ad-b8e0c0b19222/.../gcc-4.6.x-glibc-2.11.1-grte-cxx11-k8-opt/bin/..././tmp/cached-robolectric-classes.jar (No such file or directory)
	at com.xtremelabs.robolectric.bytecode.ClassCache.saveAllClassesToCache(ClassCache.java:118)
	at com.xtremelabs.robolectric.bytecode.ClassCache.run(ClassCache.java:55)
Caused by: java.io.FileNotFoundException: /.../workspace/75f9c8e32bf7255ed0bca1d210550685/2f2babb5-87c5-408b-85ad-b8e0c0b19222/.../gcc-4.6.x-glibc-2.11.1-grte-cxx11-k8-opt/bin/..././tmp/cached-robolectric-classes.jar (No such file or directory)
at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:209) at java.io.FileOutputStream.<init>(FileOutputStream.java:160) at com.xtremelabs.robolectric.bytecode.ClassCache.saveAllClassesToCache(ClassCache.java:110
We are using an internal build-run system, so the directory where Robolectric store the cached-robolectric-classes.jar
varies, will it affect if there's no cached-robolectric-classes.jar stored?
Thanks!

Aaron VonderHaar

unread,
Aug 16, 2012, 1:04:45 AM8/16/12
to robol...@googlegroups.com
If the cache doesn't exist, that should be fine--it will simply create
it in ./tmp/cached-robolectric-classes.jar from where the tests are
being run from. I've never personally run into this error before.
Maybe something is happening with your build system where the working
directory that the tests are running from is deleted before the test
run finishes?

Su Wang

unread,
Aug 16, 2012, 2:19:44 PM8/16/12
to robol...@googlegroups.com
Hi Aaron,
The issue "./tmp/cached-robolectric-classes.jar" disappears when I run those tests locally, it's still there when running remotely. Let's ignore this issue now because it works well when running locally.
But the issue "error inflating layout/activity_gmm" is still there when running test locally, it seems that the class "Fragment" couldn't be loaded correctly, you can see logs from my previous posts. I guess it is because Robolectric is using android.support.v4.Fragment but our project is using android.app.Fragement, but the issue still happens after I bind a ShadowFragment which shadows android.app.Fragement by myself. Can you help to analyze where the problem may be?
Thank you very much!

Aaron VonderHaar

unread,
Aug 17, 2012, 2:55:32 AM8/17/12
to robol...@googlegroups.com
The line mentioned in your stack trace
https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/bytecode/RobolectricClassLoader.java#L54
seems to be using the name passed in to it.

Does your <fragment> element in your activity_gmm layout file have a
valid class attribute?

Su Wang

unread,
Aug 17, 2012, 3:53:40 AM8/17/12
to robol...@googlegroups.com
Hi Aaron,
Thanks a lot, the error disappears after adding a name to my fragment.
But now I get the error:
Caused by: java.lang.ClassCastException: com.xxx.MyFragment cannot be cast to android.support.v4.app.Fragment
	at com.xtremelabs.robolectric.res.ViewLoader.constructFragment(ViewLoader.java:193)
	at com.xtremelabs.robolectric.res.ViewLoader.create(ViewLoader.java:174)
	at com.xtremelabs.robolectric.res.ViewLoader.inflate(ViewLoader.java:150)
	at com.xtremelabs.robolectric.res.ViewLoader.inflate(ViewLoader.java:153)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
	at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
	at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:372)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
	at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:616)
	at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:99)
	at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
	at android.view.LayoutInflater.inflate(LayoutInflater.java)
	at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:102)

It seems this issue really relates to the "different fragment classes", my fragment class extends from android.app.Fragment, and it seems the ViewLoader of Robolectric only constructs android.support.v4.Fragment, so is there anyway to get a solution or a workaround? Or I could only wait for Robolectric to support android.app.Fragment?
Thanks again!

Aaron VonderHaar

unread,
Aug 19, 2012, 5:22:29 PM8/19/12
to robol...@googlegroups.com
I suppose one workaround would be to switch to using
android.support.v4.app.Fragment in your code. I haven't personally
worked on a project with fragments that didn't use the support
library, so I'm afraid that's all I can suggest.

Maybe someone else on the list can share their experiences with using
android.app.Fragment?

Robolectric should ultimately support both Fragment classes, but as
far as I know there isn't a consensus yet on how best to approach
implementing this. (I believe there was a brief email thread a couple
months ago about this?)

Su Wang

unread,
Aug 20, 2012, 8:38:05 PM8/20/12
to robol...@googlegroups.com
Thanks a lot!

Peter T

unread,
Feb 18, 2013, 11:44:24 AM2/18/13
to robol...@googlegroups.com
Is there any word on non-support library (android.app.Fragment) Fragment handling in Robolectric? I've just tried against the 2.0-alpha-1 release and it doesn't seem to be in there, but there perhaps a pull request awaiting review?

cheers

Christian Williams

unread,
Feb 27, 2013, 3:14:16 AM2/27/13
to robol...@googlegroups.com
Working on it for alpha 3.

--X [typos courtesy of my iPhone]
--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Su Wang

unread,
Feb 27, 2013, 12:36:43 PM2/27/13
to robol...@googlegroups.com
Great!
Is there a time line for alpha 3?
Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/A1oJ_jtIRSI/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages