嗨,我有一个 xml 布局,我已将其作为片段包含在每个布局中。
这是我在 maincategories.xml 中的 xml
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/mainbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:src="@drawable/logo" />
</LinearLayout>
<View
android:id="@+id/viewLine"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@id/txtUserName"
android:background="#cccccc" />
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"
android:listSelector="@drawable/list_selector"
android:smoothScrollbar="true" />
<fragment
android:name="com.rag.xmlparsing.fragmentactivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
上述 xml 的 java 代码是
public class Listofcategories extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maincategories);
// some code for list activity
}
而fragmentactivity类是
public class fragmentactivity extends FragmentActivity {
RadioButton radioButton;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.actnavbar,
container, false);
radioButton = (RadioButton) view.findViewById(R.id.home);
radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton = (RadioButton) view.findViewById(R.id.aboutus);
radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton = (RadioButton) view.findViewById(R.id.contactus);
radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton = (RadioButton) view.findViewById(R.id.orderhistory);
radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
return view;
}
private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int sid=buttonView.getId();
int hme=R.id.home;
int abtus=R.id.aboutus;
int cnt=R.id.contactus;
int ordr=R.id.orderhistory;
if (isChecked) {
if(sid==hme)
{
//some function
}
else if(sid==abtus)
{
//some function
}
else if(sid==cnt)
{
//
}
else if(sid==ordr)
{
}
}
}
};
}
和我的 actnavbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="@android:color/white"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="5dp"
android:background="#cccccc"
>
<RadioButton
android:id="@+id/home"
style="@style/navbar_button"
android:drawableTop="@drawable/home_icon"
android:text="Home"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:textSize="12sp"
android:textColor="#333333"
/>
<RadioButton
android:id="@+id/aboutus"
style="@style/navbar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:textSize="12sp"
android:textColor="#333333"
android:drawableTop="@drawable/aboutus_icon"
android:gravity="center_horizontal" />
<RadioButton
android:id="@+id/contactus"
style="@style/navbar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Contact us"
android:textColor="#333333"
android:drawableTop="@drawable/contact_icon"
android:gravity="center_horizontal" />
<RadioButton
android:id="@+id/orderhistory"
style="@style/navbar_button"
android:drawableTop="@drawable/order_history_icon"
android:text="Order history"
android:textSize="12sp"
android:textColor="#333333"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
/>
</RadioGroup>
</RelativeLayout>
如果我这样使用,我会收到以下 logcat 错误
09-21 14:52:12.466: E/AndroidRuntime(336): FATAL EXCEPTION: main
09-21 14:52:12.466: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.Listofcategories}: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-21 14:52:12.466: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 14:52:12.466: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
09-21 14:52:12.466: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-21 14:52:12.466: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-21 14:52:12.466: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
09-21 14:52:12.466: E/AndroidRuntime(336): Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-21 14:52:12.466: E/AndroidRuntime(336): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.Activity.setContentView(Activity.java:1657)
09-21 14:52:12.466: E/AndroidRuntime(336): at com.androidhive.xmlparsing.Listofcategories.onCreate(Listofcategories.java:97)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-21 14:52:12.466: E/AndroidRuntime(336): ... 11 more
09-21 14:52:12.466: E/AndroidRuntime(336): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.androidhive.xmlparsing-1.apk]
09-21 14:52:12.466: E/AndroidRuntime(336): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-21 14:52:12.466: E/AndroidRuntime(336): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-21 14:52:12.466: E/AndroidRuntime(336): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
09-21 14:52:12.466: E/AndroidRuntime(336): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
09-21 14:52:12.466: E/AndroidRuntime(336): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-21 14:52:12.466: E/AndroidRuntime(336): ... 20 more
我在哪里做错了???请任何建议..