現在まで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 でビルドした場合、プロジェクトはまったく同じコードで正常に動作します。