我有一个活动,其中包含三个片段,两个具有listView
布局,一个具有Relative layout
.
这是活动的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/activity_main_large"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" xmlns:tools="http://schemas.android.com/tools">
<fragment
android:id="@+id/menu_category_fragment"
android:name="com.thesis.menubook.MenuCategory"
android:layout_width="123dp"
android:layout_height="match_parent"
tools:layout="@layout/activity_menu_category" />
<fragment
android:id="@+id/menu_fragment"
android:name="com.thesis.menubook.MenuFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.35"
tools:layout="@layout/activity_menu_fragment" />
<fragment
android:id="@+id/orderlist_fragment"
android:name="com.thesis.menubook.OrderListFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.08"
tools:layout="@layout/activity_order_list" />
</LinearLayout>
这是主要活动的java文件
package com.thesis.menubook;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MenuMain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_menu_main, menu);
return true;
}
}
在那里,我只是试图加载空片段以查看它们是否会实际加载,然后添加小部件和代码以将数据导入片段。但我收到一条错误消息java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}:
有什么我忽略的原因是它无法加载空片段的原因吗?
这是我的 LogCat(更正类名后更新):
02-12 23:04:28.369: E/AndroidRuntime(1143): FATAL EXCEPTION: main
02-12 23:04:28.369: E/AndroidRuntime(1143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.menubook/com.thesis.menubook.MenuMain}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.os.Looper.loop(Looper.java:132)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread.main(ActivityThread.java:4025)
02-12 23:04:28.369: E/AndroidRuntime(1143): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 23:04:28.369: E/AndroidRuntime(1143): at java.lang.reflect.Method.invoke(Method.java:491)
02-12 23:04:28.369: E/AndroidRuntime(1143): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-12 23:04:28.369: E/AndroidRuntime(1143): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-12 23:04:28.369: E/AndroidRuntime(1143): at dalvik.system.NativeStart.main(Native Method)
02-12 23:04:28.369: E/AndroidRuntime(1143): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
02-12 23:04:28.369: E/AndroidRuntime(1143): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.Activity.setContentView(Activity.java:1780)
02-12 23:04:28.369: E/AndroidRuntime(1143): at com.thesis.menubook.MenuMain.onCreate(MenuMain.java:15)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
02-12 23:04:28.369: E/AndroidRuntime(1143): ... 11 more
02-12 23:04:28.369: E/AndroidRuntime(1143): Caused by: java.lang.ClassCastException: com.thesis.menubook.MenuCategory cannot be cast to android.app.Fragment
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.Fragment.instantiate(Fragment.java:493)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.Fragment.instantiate(Fragment.java:468)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.app.Activity.onCreateView(Activity.java:4132)
02-12 23:04:28.369: E/AndroidRuntime(1143): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
02-12 23:04:28.369: E/AndroidRuntime(1143): ... 20 more
02-12 23:04:28.905: D/dalvikvm(1143): GC_CONCURRENT freed 329K, 6% free 7390K/7815K, paused 12ms+20ms
02-12 23:04:34.180: I/Process(1143): Sending signal. PID: 1143 SIG: 9**
MenuCategory.java
package com.thesis.menubook;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MenuCategory extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_menu_category, container, false);
return view;
}
}
MenuFragment.java
package com.thesis.menubook;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MenuFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_menu_fragment, container, false);
return view;
}
}
OrderListFragment.java
package com.thesis.menubook;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OrderListFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_order_list, container, false);
return view;
}
}
以下是片段的 xml 文件:
活动菜单类别
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
活动菜单片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="bottom|top" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world"
android:paddingTop="20dp" />
</RelativeLayout>
活动订单列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Name Label -->
<TextView
android:id="@+id/categoryname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>