gpt4 book ai didi

android - 调用 fragment 时 Activity 被破坏

转载 作者:搜寻专家 更新时间:2023-11-01 08:18:52 24 4
gpt4 key购买 nike

在子类的类中使用 fragment RoboActivity, fragment 时崩溃应用程序 交易开始;

我也无法使用底部导航或 Tablayout,可能跟roboguice有关。

这是 Obd 汽车扫描应用程序

代码如下:

  public void onCreate(Bundle savedInstanceState) 

{
super.onCreate(savedInstanceState);

SpeedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment selectedFragment = null;
selectedFragment = new SpeedFragment();
AppCompatActivity activity=new AppCompatActivity();

if (!isFinishing() && !isDestroyed()) {
FragmentTransaction ft =activity.getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();
}
}
});
}

日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.quad14.obdnewtry, PID: 7878
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.quad14.obdnewtry.activity.MainActivity$3.onClick(MainActivity.java:272)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Layout:下面只添加了Fragment相关的组件

 <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="80dp"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonlinear"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speed"
android:id="@+id/speedbtid"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rpm"
android:id="@+id/rpmbtid"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Km"
android:id="@+id/distanbtid"/>

</LinearLayout>

</LinearLayout>

最佳答案

如果您的 Activity 扩展了 Activity,您的 fragment 应该是 android.app.Fragment。然后使用下面的代码

 android.app.FragmentTransaction ft =getFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();

否则,如果您的 Activity 扩展 AppCompactActivity,您的 fragment 应该是 android.support.v4.app.Fragment。那么这段代码将起作用

 FragmentTransaction ft =getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();

关于android - 调用 fragment 时 Activity 被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com