Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call fragment from an activity in android?

I am working on android fragments. In my app I am getting an issue with activity and fragment. i.e I created a button in fragment1 and called an activity1 in that button click. Again in activity1 I created another button and called activity2. Now from activity2 I called activity1 and from activity1 I am trying to call fragment1. But it is showing error at id(can anyone tell me which id we need to give). I researched in google on how to call fragment from activity but I didnt find any solution for this. Please help me out with this issue. Thanks in advance.

My code:

Activity1.class

public void back(View v) {

 MyFragment1 fragmentS1 = new MyFragment1();
 getFragmentManager().beginTransaction().replace(android.R.id.frame_container, fragmentS1).commit();

}

Logcat Error:

04-10 10:55:28.876: E/AndroidRuntime(22103): FATAL EXCEPTION: main
04-10 10:55:28.876: E/AndroidRuntime(22103): java.lang.IllegalArgumentException: No view found for id 0x7f0a0004 (com.vizteck.navigationdrawer:id/frame_container) for fragment Fragment1{429cba90 #0 id=0x7f0a0004}
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:895)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.os.Handler.handleCallback(Handler.java:725)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.os.Looper.loop(Looper.java:137)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at android.app.ActivityThread.main(ActivityThread.java:5283)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at java.lang.reflect.Method.invokeNative(Native Method)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at java.lang.reflect.Method.invoke(Method.java:511)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-10 10:55:28.876: E/AndroidRuntime(22103):    at dalvik.system.NativeStart.main(Native Method)
like image 304
Amrutha Avatar asked Apr 10 '14 04:04

Amrutha


1 Answers

Write it in your activity like below

Fragment fragment = new YourFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.output1, fragment);
transaction.addToBackStack(null);
transaction.commit();
like image 135
Maksudul Hasan Raju Avatar answered Oct 11 '22 03:10

Maksudul Hasan Raju