【问题标题】:side by side fragments on tablet平板电脑上的并排碎片
【发布时间】:2013-01-24 23:37:56
【问题描述】:

我有一个显示地图的应用程序。当用户按下其中一个操作栏时,会出现另一个列表视图片段,其中显示项目列表。

当应用程序在平板电脑上运行时,我试图让列表视图出现在地图的左侧,但是我遇到了困难。

我目前有一个管理片段的 Activity。

我尝试了在 StackOverflow 上找到的解决方案,但它不起作用。

这是用于大型布局和标准布局的布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ViewGroup
    android:id="@+id/leftContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

<ViewGroup
    android:id="@+id/rightContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2" />

</LinearLayout> 

这是活动的 onCreate 代码: super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

ft.add(R.id.leftContainer, loadRoutesFragment);
ft.add(R.id.rightContainer, viewModeFragment);
ft.hide(loadRoutesFragment);
ft.commit();

我一开始就隐藏了 loadRoutesFragment,但是当按下按钮时,我使用了 show 方法,该方法应该使 Fragment 出现。

应用程序在setContentView(R.layout.activity_main); 上崩溃 以下是例外情况:

01-24 23:25:56.889: E/AndroidRuntime(20746): FATAL EXCEPTION: main
01-24 23:25:56.889: E/AndroidRuntime(20746): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.srose.cyclopathed/com.srose.cyclopathed.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.view.ViewGroup
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.os.Looper.loop(Looper.java:137)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at java.lang.reflect.Method.invoke(Method.java:511)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at dalvik.system.NativeStart.main(Native Method)
01-24 23:25:56.889: E/AndroidRuntime(20746): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.view.ViewGroup
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at com.srose.cyclopathed.MainActivity.onCreate(MainActivity.java:90)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.Activity.performCreate(Activity.java:5104)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-24 23:25:56.889: E/AndroidRuntime(20746):    ... 11 more
01-24 23:25:56.889: E/AndroidRuntime(20746): Caused by: java.lang.InstantiationException: can't instantiate class android.view.ViewGroup
01-24 23:25:56.889: E/AndroidRuntime(20746):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-24 23:25:56.889: E/AndroidRuntime(20746):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-24 23:25:56.889: E/AndroidRuntime(20746):    ... 26 more

有人知道为什么吗?

谢谢

当前 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/leftContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

<fragment
    android:id="@+id/rightContainer"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2" />

</LinearLayout> 

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    LogCat 读取:

    Caused by: java.lang.InstantiationException: can't instantiate class android.view.ViewGroup
    

    这意味着您不能在布局中创建通用 ViewGroup。一个常见的替代方法是使用 &lt;fragment /&gt; 元素和 name 属性中的类,或者您可以使用特定的 ViewGroup,如 FrameLayout 或 LinearLayout。 fragments 上的开发者指南中有一些示例。

    【讨论】:

    • 感谢您的回复。我将 ViewGroup 更改为保持相同参数的片段。它似乎与开发人员指南示例相同,但是相同的问题仍然存在,但存在相同的异常,只是它不能膨胀类片段
    • 请将您当前的 XML 添加到您的问题中。
    • 我正在使用 ActionBarSherlocks SherlockFragment。这会导致问题吗?
    • 不应该,你还没有定义你要使用什么片段类。您必须添加:android:name="com.srose.cyclopathed.LoadRoutesFragment"android:name="com.srose.cyclopathed.ViewModeFragment"(假设它们是适当的类名。)
    • 谢谢,现在好像可以了。不过有一件事。即使我不添加 loadRoutesFragment,左侧面板也会存在。在它上面调用 hide 方法也不起作用。我是否需要指定一些内容以便仅在按下按钮时显示侧面板?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多