开发者

DIfferent fragments for different orientation

I have an Activity in a tablet app and I want to show different Fragments depending on the orientation. I figured the easiest way to do this was just to define two different layout XML files, one for landscape and one for portrait. In other words I have /layout/home.xml which looks like this:

<LinearLayout android:orientation="horizontal">
  <fragment class="com.foo.app.Frag1" android:id="frag1"/>
  <fragment class="com.foo.app.Frag2" android:id="frag2"/>
</LinearLayout>

And then in /layout-port/home.xml:

<LinearLayout android:orientation="vertical">
  <fragment class="com.foo.app.Frag2" android:id="frag2"/>
  <fragment class="com.foo.app.Frag3" android:id="frag3"/>
</LinearLayout>

When I run it, I ca开发者_如何转开发n start in landscape and go to portrait, and everything is fine. However, when I go from portrait to landscape, the app crashes with a android.content.res.Resources$NotFoundException with Resource ID equal to "frag3." That's right it's looking for Frag3 when going to landscape made, and there is no Frag3 in that mode. This happens during the Activity.onCreate stack, so before any of my code has any chance to make the app crash.

Are Fragments not supposed to work this way? Do I need to use a FragmentTransaction instead?


It's hard to say without seeing the code- However, Resources.NotFoundException usually means you tried to look up an asset (think R.layout.xx, R.drawable.xx, etc) incorrectly.

That said, I tried writing a small app using your layout XML, and the compiler yelled at me for hardcoding strings as the ID's when I copied in your XML code to test it out. Try switching the values of the android:id attribute to read like this:

android:id="android:id="@+id/frag1"

that might fix the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜