android-fragments 如何在android中调用Activity中的片段?

slsn1g29  于 2022-11-14  发布在  Android
关注(0)|答案(5)|浏览(254)

我正在使用android fragments。我的应用程序中的activityfragment出现了问题。例如,我在fragment1中创建了一个button,并在button单击中调用了一个activity1。在activity1中,我再次创建了另一个button,并调用了activity2。现在从activity2开始,我打电话给activity1,我试图从activity1打电话给fragment1。但它显示一个错误的id(谁能告诉我,我们需要给予哪个id)。我研究了谷歌如何从activity打电话给fragment,但我没有找到任何解决这个问题的方法。请帮助我解决这个问题。提前感谢。
我的代码:
Activity1.class

public void back(View v) {
MyFragment1 fragmentS1 = new MyFragment1();
getFragmentManager().beginTransaction().replace(android.R.id.frame_container, fragmentS1).commit();
}

日志目录错误:

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)
iyfamqjs

iyfamqjs1#

试试这个..
从此android.R.id.frame_container中删除android
如果您使用的是activity1延伸Activity,请使用getFragmentManager(),或者如果您使用的是activity1延伸FragmentActivity,请使用getSupportFragmentManager()
如下所示

MyFragment1 fragmentS1 = new MyFragment1();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragmentS1).commit();
az31mfrm

az31mfrm2#

Fragment frag;
FragmentManager fm1 = MainActivity.this
    .getSupportFragmentManager();
FragmentTransaction ft1 = fm1.beginTransaction();
frag = new Animal_Activity();
ft1.replace(R.id.activity_main_content_fragment, frag);
ft1.commit();
rur96b6h

rur96b6h3#

将其写在您的活动中,如下所示

Fragment fragment = new YourFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.output1, fragment);
transaction.addToBackStack(null);
transaction.commit();
agyaoht7

agyaoht74#

简单地你可以这样称呼:

Fragment fragment = new FragmentName(); 
FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_main, fragment).commit();
au9on6nz

au9on6nz5#

请执行以下操作:

FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            MyFragment1 fragmentS1 = new MyFragment1();
            fragmentTransaction.replace(R.id.realtabcontent, mBarcodeScreen);
            fragmentTransaction.commit();

祝你好运!

相关问题