我正在尝试创建一个可以与 android sdk 7 一起使用的日历应用程序,直到现在。我想使用标签来显示不同的日历视图(每周、每月、每日、议程)。因此,我需要为每个视图使用片段并使用支持库来实现向后兼容性。
这是我的 xml 布局文件
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
这是随之而来的活动
package com.bmooredev.bpassistant;
import com.bmooredev.bpassistant.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
public class EventsActivity extends FragmentActivity{
private WeekFragment FragWeek=new WeekFragment();
private MonthFragment FragMonth=new MonthFragment();
private DayFragment FragDay=new DayFragment();
private AgendaFragment FragAgenda=new AgendaFragment();
private int month;
private int day;
private int year;
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
WeekFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
MonthFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
DayFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
AgendaFragment.class, null);
showFragWeek();
}
private void showFragWeek(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent, FragWeek);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void showFragMonth(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent, FragMonth);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void showFragDay(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent, FragDay);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void showFragAgenda(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent, FragAgenda);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
}
这是我运行 logcat 时显示的内容
03-01 10:07:28.655: E/AndroidRuntime(6814): FATAL EXCEPTION: main
03-01 10:07:28.655: E/AndroidRuntime(6814): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bmooredev.bpassistant/com.bmooredev.bpassistant.EventsActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.app.FragmentTabHost
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.os.Looper.loop(Looper.java:137)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-01 10:07:28.655: E/AndroidRuntime(6814): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 10:07:28.655: E/AndroidRuntime(6814): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 10:07:28.655: E/AndroidRuntime(6814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-01 10:07:28.655: E/AndroidRuntime(6814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-01 10:07:28.655: E/AndroidRuntime(6814): at dalvik.system.NativeStart.main(Native Method)
03-01 10:07:28.655: E/AndroidRuntime(6814): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.app.FragmentTabHost
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-01 10:07:28.655: E/AndroidRuntime(6814): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.Activity.setContentView(Activity.java:1867)
03-01 10:07:28.655: E/AndroidRuntime(6814): at com.bmooredev.bpassistant.EventsActivity.onCreate(EventsActivity.java:26)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.Activity.performCreate(Activity.java:5008)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-01 10:07:28.655: E/AndroidRuntime(6814): ... 11 more
03-01 10:07:28.655: E/AndroidRuntime(6814): Caused by: java.lang.ClassNotFoundException: android.support.v4.app.FragmentTabHost
03-01 10:07:28.655: E/AndroidRuntime(6814): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-01 10:07:28.655: E/AndroidRuntime(6814): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-01 10:07:28.655: E/AndroidRuntime(6814): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
03-01 10:07:28.655: E/AndroidRuntime(6814): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-01 10:07:28.655: E/AndroidRuntime(6814): ... 20 more
此外,如果我使用 2.2 构建项目,该项目使用完全相同的代码运行良好。