I am working on android fragment when i run my app in android 3.0 versions and above it’s working fine but when i am trying to run below 3.0 it’s giving forceclose error i am not understanding what is the wrong. Below is log cat errors and my code.
And if run my app in smart phone then i need to display view in another windown with out opening new activity. can you guide me how to do this. And it’s working fine in tablet. I am sending working screen shot for tablet.
01-21 09:29:24.272: E/AndroidRuntime(398): FATAL EXCEPTION: main
01-21 09:29:24.272: E/AndroidRuntime(398): java.lang.NoSuchMethodError: com.example.fragment_demo.MainActivity.getFragmentManager
01-21 09:29:24.272: E/AndroidRuntime(398): at com.example.fragment_demo.MainActivity.onCreate(MainActivity.java:19)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.os.Looper.loop(Looper.java:123)
01-21 09:29:24.272: E/AndroidRuntime(398): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-21 09:29:24.272: E/AndroidRuntime(398): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 09:29:24.272: E/AndroidRuntime(398): at java.lang.reflect.Method.invoke(Method.java:507)
01-21 09:29:24.272: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-21 09:29:24.272: E/AndroidRuntime(398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-21 09:29:24.272: E/AndroidRuntime(398): at dalvik.system.NativeStart.main(Native Method)
public class MainActivity extends Activity {
Button b1, b2, b3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fdemo);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
StartFragment sf = new StartFragment();
ft.add(R.id.myFragment, sf);
ft.commit();
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b1.setOnClickListener(onButtonClick);
b2.setOnClickListener(onButtonClick);
b3.setOnClickListener(onButtonClick);
}
Button.OnClickListener onButtonClick = new Button.OnClickListener(){
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Fragment fragment = null;
if(v == b1){
fragment = new Fragment1();
}else if(v == b2){
fragment = new Fragment2();
}else if(v == b3){
fragment = new Fragment3();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.myFragment, fragment);
ft.commit();
}
};
}
main_fdemo.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="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 3" />
</LinearLayout>
<LinearLayout
android:id="@+id/myFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3" >
</LinearLayout>
</LinearLayout>
public class StartFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// TODO Auto-generated method stub
return inflater.inflate(R.layout.start_fragment, container, false);
}
}
start_fragment.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="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Select the Fragments"
android:textSize="20dp" />
</LinearLayout>
public class Fragment1 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
fragment_1.xml code:-
<?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="match_parent"
android:background="#F97C7C"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Select the Fragments"
android:textSize="20dp" />
</LinearLayout>

You need to use the Android Support Library to be backwards compatible down to API 4. While using the library you will use support methods like getSupportFragmentManager() instead of getFragmentManager().
http://developer.android.com/training/basics/fragments/support-lib.html