Very simple code, but got error quot;Activity has been destroyedquot; when use Fragment (非常简单的代码,但出现错误“活动已被破坏使用片段时)
问题描述
我制作了一个非常简单的 Activity,它显示了一个简单的 ListFragment,如下所示:
I made a very simple Activity which shows a simple ListFragment like below:
我的活动:
public class MyActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
FragmentManager fragMgr = getSupportFragmentManager();
FirstFragment list = new FirstFragment();
fragMgr.beginTransaction().add(android.R.id.content, list).commit();
}
}
我的 ListFragment:
My ListFragment:
public class FirstFragment extends ListFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String listContent[] = {"Larry", "Moe", "Curly"};
setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, listContent));
}
...
}
当我启动我的应用时,我收到了错误消息:
When I start my app, I got error message:
java.lang.IllegalStateException: Activity has been destroyed
E/AndroidRuntime( 947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 947): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 947): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 947): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 947): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 947): at java.lang.reflect.Method.invokeNative(Native Method)
...
它抱怨Activity已被销毁,为什么???
附:main.xml:
P.S. main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="next" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data" />
</LinearLayout>
推荐答案
我也遇到了类似的问题.
我意识到这是因为活动在 FragmentTransaction
即将获取 .commit()
时被破坏.
I also faced a similar problem.
I realized that this happened because the activity was being destroyed while the FragmentTransaction
was about to get .commit()
.
解决这个问题的方法是检查 Activity.isFinishing()
是否为真.
A solution to this was to check whether the Activity.isFinishing()
is true or not.
if (!isFinishing()) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(SOME_RES_ID, myFragmentInstance);
ft.commit();
}
这篇关于非常简单的代码,但出现错误“活动已被破坏"使用片段时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:非常简单的代码,但出现错误“活动已被破坏"使用片段时
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01