简体   繁体   English

使用片段销毁活动

[英]Activity destroyed using Fragment

I have a ListView in ListView.class , when the User clicks on one item (let's say item at position =0) Fragment6 should be opened. 我有一个ListView ListView.class ,当用户点击一个项目(假设在位置项= 0)Fragment6应打开。

Additionally, I have a HandleListClick.class that gets the position ( eg position = 0) and then uses a switch to open a fragment, but I get an error. 另外,我有一个HandleListClick.class获取位置(例如position = 0),然后使用switch打开片段,但出现错误。

java.lang.IllegalStateException: Activity has been destroyed java.lang.IllegalStateException:活动已被破坏

How can I handle this annoying problem? 我该如何解决这个烦人的问题? My activity is declared in Manifest. 我的活动在清单中声明。

Here is my ListView.class 这是我的ListView.class

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // ListView Clicked item index
            int itemPosition     = position;

            // ListView Clicked item value
            String  itemValue    = (String) listView.getItemAtPosition(position);


                if (position==0){
                    HandleListClick handleListClick = new HandleListClick();
                    handleListClick.getItemPosition(0);
                }
}

Here is my handleListClick.class 这是我的handleListClick.class

public class HandleListClick extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_handle_list_click);
}


public void getItemPosition(int position) {
    switch (position) {
        case 0:
            Fragment6 frag6 = null;
            frag6 = new Fragment6();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.framelayout, frag6)
                    .commit();


    }
}

} }

Here is my Fragment6.java 这是我的Fragment6.java

public class Fragment6 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

   View v = inflater.inflate(R.layout.fragmentdescrip6, container,false);
    return v;



 //  ViewGroup rootView = (ViewGroup) inflater.inflate(
  //         R.layout.fragmentdescrip6, container, false);

 //  return rootView;
}

} }

Here is activity_handle_list_click.xml 这是activity_handle_list_click.xml

     <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.app.HandleListClick">


    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id= "@+id/framelayout"
        >


    </FrameLayout>

</android.support.constraint.ConstraintLayout>

And here is fragmentdescrip6.xml 这是fragmentdescrip6.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag6"

    >

    <TextView
        android:id="@+id/textfrag6label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="foo"
        android:textStyle="bold"
        android:textColor="#FFFF4444"
        android:textSize="20dp"
        android:layout_marginTop="17dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textfrag6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textfrag6label"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="33dp"
        android:textStyle="bold"
        android:text="some Text" />


</RelativeLayout>

Try it this way 这样尝试

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

                // ListView Clicked item index
                int itemPosition     = position;

                // ListView Clicked item value
                String  itemValue    = (String) listView.getItemAtPosition(position);


                if (position==0){
                    Intent intent = new Intent(CurrentActivity.this, HandleListClick.class);
                    intent.putExtra("POSITION", position);
                    startActivity(intent);
                }
                   /* HandleListClick handleListClick = new HandleListClick();
                    handleListClick.getItemPosition(0);*/
                }
            }

Change the Activity code 更改活动代码

    public class HandleListClick extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_handle_list_click);
            Intent intent = getIntent();
            int pos = intent.getIntExtra("POSITION",0);
            getItemPosition(pos);
        }


        public void getItemPosition(int position) {
            switch (position) {
                case 0:
                    Fragment6 frag6 = null;
                    frag6 = new Fragment6();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.framelayout, frag6)
                            .commit();


            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选项卡片段错误发生,并且片段父活动已被破坏 - Tab fragment error occurring and fragment parent activity has been destroyed 覆盖片段内的活动工具栏(并在片段被销毁时恢复) - Override the activity toolbar inside a fragment (and restore when the fragment is destroyed) 尝试替换片段时出错,片段事务中的活动被破坏 - Error trying to replace a Fragment, Activity Destroyed in fragment transaction 仅当主机活动被破坏时如何从片段调用方法? - How to call method from fragment only if host activity was destroyed? Android片段-在片段中按文本后,“活动已被破坏” - Android Fragments - “Activity has been destroyed” after pressing text in a fragment 为什么在添加片段(#2)后我的活动遭到破坏? - Why does my Activity get destroyed upon adding a fragment (#2)? 当Activity被销毁时,片段监听器不起作用 - Fragment Listener doesn't works when Activity is destroyed 为什么添加片段会破坏我的活动? - Why does my Activity get destroyed on adding a fragment? 在片段中使用活动方法 - Using Activity methods in Fragment Android:片段堆栈和破坏的活动在onResume之后导致空指针异常 - Android: Fragment stack and destroyed activity causing null pointer exception after onResume
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM