0

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

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

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

我尝试了在 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> 
4

1 回答 1

0

LogCat 显示:

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

这意味着您不能在布局中创建通用 ViewGroup。一种常见的替代方法是<fragment />在属性中使用具有类的元素,name或者您可以使用特定的 ViewGroup,如 FrameLayout 或 LinearLayout。开发人员指南中有一些关于片段的示例。

于 2013-01-24T23:50:11.680 回答