簡単なフラグメントの例を作成すると、以下のエラーが発生します。
09-05 07:57:28.570: E/AndroidRuntime(1138): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragment_one.f_one: make sure class name exists, is public, and has an empty constructor that is public
09-05 08:57:03.058: E/AndroidRuntime(1477): FATAL EXCEPTION: main
09-05 08:57:03.058: E/AndroidRuntime(1477): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments_one/com.example.fragments_one.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.os.Looper.loop(Looper.java:137)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-05 08:57:03.058: E/AndroidRuntime(1477): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:57:03.058: E/AndroidRuntime(1477): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 08:57:03.058: E/AndroidRuntime(1477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-05 08:57:03.058: E/AndroidRuntime(1477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-05 08:57:03.058: E/AndroidRuntime(1477): at dalvik.system.NativeStart.main(Native Method)
09-05 08:57:03.058: E/AndroidRuntime(1477): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-05 08:57:03.058: E/AndroidRuntime(1477): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.Activity.setContentView(Activity.java:1835)
09-05 08:57:03.058: E/AndroidRuntime(1477): at com.example.fragments_one.MainActivity.onCreate(MainActivity.java:12)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.Activity.performCreate(Activity.java:4465)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-05 08:57:03.058: E/AndroidRuntime(1477): ... 11 more
09-05 08:57:03.058: E/AndroidRuntime(1477): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragment_one.f_one: make sure class name exists, is public, and has an empty constructor that is public
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
09-05 08:57:03.058: E/AndroidRuntime(1477): ... 21 more
09-05 08:57:03.058: E/AndroidRuntime(1477): Caused by: java.lang.ClassNotFoundException: com.example.fragment_one.f_one
09-05 08:57:03.058: E/AndroidRuntime(1477): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-05 08:57:03.058: E/AndroidRuntime(1477): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-05 08:57:03.058: E/AndroidRuntime(1477): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-05 08:57:03.058: E/AndroidRuntime(1477): at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
09-05 08:57:03.058: E/AndroidRuntime(1477): ... 24 more
主な活動
package com.example.fragments_one;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/f_one"
android:name="com.example.fragment_one.f_one"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/f_two"
android:name="com.example.fragment_one.f_two"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
フラグメント レイアウト
1.f_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
f_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFE00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
私のフラグメントクラス
package com.example.fragments_one;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class f_one extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ---Inflate the layout for this fragment---
return inflater.inflate(R.layout.f_one, container, false);
}
}
package com.example.fragments_one;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class f_two extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// ---Inflate the layout for this fragment---
return inflater.inflate(R.layout.f_two, container, false);
}
}