我有一个显示地图的应用。当用户按下其中一个操作栏时,会出现另一个列表视图片段,显示项目列表。
我正试图让应用程序在平板电脑上运行时,列表视图显示在地图的左侧,但是我遇到了困难。
我目前有一个管理片段的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>
以下是ACtivity的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方法,该方法应该显示片段。
应用程序在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>
答案 0 :(得分:0)
LogCat读取:
Caused by: java.lang.InstantiationException: can't instantiate class android.view.ViewGroup
这意味着您无法在布局中创建通用ViewGroup。一个常见的替代方法是在<fragment />
属性中使用name
元素和类,或者可以使用特定的ViewGroup,如FrameLayout或LinearLayout。在fragments上的开发人员指南中有一些示例。