gpt4 book ai didi

java - 使用 Fragment 销毁 Activity

转载 作者:行者123 更新时间:2023-11-29 19:12:24 25 4
gpt4 key购买 nike

我在 ListView.class 中有一个 ListView,当用户单击一个项目(假设项目位于位置 =0)时,应打开 Fragment6。

另外,我有一个 HandleListClick.class 获取位置(例如 position = 0),然后使用 switch 打开一个 fragment ,但我得到一个错误.

java.lang.IllegalStateException: Activity has been destroyed

我该如何处理这个恼人的问题?我的 Activity 在 list 中声明。

这是我的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);
}
}

这是我的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();


}
}

这是我的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;
}

这里是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>

这里是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>

最佳答案

这样试试

 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);*/
}
}

更改 Activity 代码

    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();


}
}
}

关于java - 使用 Fragment 销毁 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864044/

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