我在活动中加载Fragment时收到以下错误
java.lang.IllegalStateException: Activity has been destroyed
03-19 14:12:48.779 12008-12008/com.ensis.sample W/System.err: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2075)
03-19 14:12:48.780 12008-12008/com.ensis.sample W/System.err: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:680)
03-19 14:12:48.781 12008-12008/com.ensis.sample W/System.err: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:634)
03-19 14:12:48.782 12008-12008/com.ensis.sample W/System.err: at com.ensis.sample.MainActivity.loadFragment(MainActivity.java:107)
03-19 14:12:48.783 12008-12008/com.ensis.sample W/System.err: at com.ensis.sample.MainActivity.clickedItem(MainActivity.java:123)
03-19 14:12:48.783 12008-12008/com.ensis.sample W/System.err: at com.ensis.sample.MyAdapter$1.onClick(MyAdapter.java:42)
实际上我有一个水平recyclerview
。当我点击recyclerview
项时,我将在该活动本身内添加片段。
截图: -
下面是我的MainActivity。
public class MainActivity extends AppCompatActivity implements CallBackInterface {
private RecyclerView recyclerView;
private MyAdapter adapter;
private List<String> listItems;
private FrameLayout frameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_id);
frameLayout = findViewById(R.id.frame_id);
// loadFragment(new AlphabetsFragment(), "Aa");
//loadFragment(this, new AlphabetsFragment(), true, null, "Aa");
loadFragment(this, new AlphabetsFragment(), false, null, "Aa");
LinearLayoutManager horizontalLayoutManager
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);
listItems = new ArrayList<>();
listItems.add("A");
listItems.add("B");
listItems.add("C");
listItems.add("D");
listItems.add("E");
listItems.add("F");
listItems.add("G");
Log.d("tag", "============click===========" + listItems.size());
adapter = new MyAdapter(listItems, this);
recyclerView.setAdapter(adapter);
}
@Override
public void clickedItem(int position) {
switch (position) {
case 0:
loadFragment(this, new AlphabetsFragment(), false, null, "Bb");
break;
case 1:
//loadFragment(new Aacf(), "Bb");
loadFragment(this, new AlphabetsFragment(), false, null, "Bb");
break;
case 2:
//loadFragment(new AlphabetsFragment(), "Cc");
loadFragment(this, new AlphabetsFragment(), false, null, "Cc");
break;
}
}
public static void loadFragment(Context context, Fragment fragment, boolean isPopBackStack,
FragmentManager.OnBackStackChangedListener onBackStackChangedListener,
String identification) {
try {
FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
if (onBackStackChangedListener != null) {
fragmentManager.addOnBackStackChangedListener(onBackStackChangedListener);
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (isPopBackStack) {
fragmentManager.popBackStack();
} else {
fragmentTransaction.addToBackStack(null);
}
Bundle bundle = new Bundle();
bundle.putString("key", identification);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frame_id, fragment);
fragmentTransaction.commit();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
下面的是我的片段代码
alphabetText = view.findViewById(R.id.text_2aid);
Bundle bundle = getArguments();
String aplhabet = bundle.getString("key");
alphabetText.setText(aplhabet);
下面是适配器类代码
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ensis on 03/19/2018.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> listItems = new ArrayList<>();
private Context context;
CallBackInterface interf = (CallBackInterface) new MainActivity();
public MyAdapter(List<String> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.activity_main2, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.button.setText(listItems.get(position));
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
interf.clickedItem(position);
}
});
}
@Override
public int getItemCount() {
Log.d("tag", "size===1===" + listItems.size());
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Button button;
public ViewHolder(View itemView) {
super(itemView);
button
= itemView.findViewById(R.id.button_id);
}
}
}
下面的是xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
android:layout_margin="16dp"
tools:context="com.ensis.sample.MainActivity">
<FrameLayout
android:id="@+id/frame_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="80dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_id"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<!--</FrameLayout>-->
</RelativeLayout>
我已经看到了与此问题相关的一些答案。但他们都没有得到我的帮助。请帮我解决这个问题。