I have below errors when creating a simple example fragment.
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
Primary activity
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>
Fragment Layouts
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>
my fragment class
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) {
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) {
user1381827
source share