我是Android新手,最近我正在使用SlidingMenu(github上的那个)。当我在单击slidemenu上的按钮时尝试更改MainActivity的片段时,我收到错误:“java.lang.IllegalStateException:活动已被破坏“。我认为它与片段有关,但无法弄清楚如何处理它。
这是日志
06-07 02:37:58.533: E/AndroidRuntime(31909): java.lang.IllegalStateException: Activity has been destroyed
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1295)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:548)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.app.BackStackRecord.commit(BackStackRecord.java:532)
06-07 02:37:58.533: E/AndroidRuntime(31909): at com.example.ui.MainActivity.switchContent(MainActivity.java:67)
06-07 02:37:58.533: E/AndroidRuntime(31909): at com.example.ui.SlidingMenuFragment.switchFragment(SlidingMenuFragment.java:110)
06-07 02:37:58.533: E/AndroidRuntime(31909): at com.example.ui.SlidingMenuFragment.onClick(SlidingMenuFragment.java:100)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.view.View.performClick(View.java:4171)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.view.View$PerformClick.run(View.java:17097)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.os.Handler.handleCallback(Handler.java:615)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.os.Looper.loop(Looper.java:137)
06-07 02:37:58.533: E/AndroidRuntime(31909): at android.app.ActivityThread.main(ActivityThread.java:4803)
06-07 02:37:58.533: E/AndroidRuntime(31909): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 02:37:58.533: E/AndroidRuntime(31909): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 02:37:58.533: E/AndroidRuntime(31909): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-07 02:37:58.533: E/AndroidRuntime(31909): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-07 02:37:58.533: E/AndroidRuntime(31909): at dalvik.system.NativeStart.main(Native Method)
以下是代码 MainActivity
public class MainActivity extends SlidingFragmentActivity implements
OnClickListener {
public static SlidingMenu slidingMenu = null;
private Fragment mContent;
private long exitTime = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initFragment();
setContentView(R.layout.activity_main);
MyApplication.getInstance().addActivity(MainActivity.this);
}
private void initFragment() {
mContent = new RidingActivityFragment();
// ridingActivityFragment = new RidingActivityFragment();
getFragmentManager().beginTransaction()
.add(R.id.content_frame, mContent).commit();
setBehindContentView(R.layout.sliding_menu);
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
Fragment aa = new SlidingMenuFragment();
fragmentTransaction.replace(R.id.content_left, aa);
fragmentTransaction.commit();
slidingMenu = getSlidingMenu();
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.title_bar_imageview:
slidingMenu.toggle(true);
break;
default:
break;
}
}
public void switchContent(Fragment fragment) {
mContent = fragment;
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
getSlidingMenu().showContent();
}
和SlidingMenuFragment
public class SlidingMenuFragment extends Fragment implements OnClickListener {
private View logoutView = null;
private View sycnView = null;
private View ridingView = null;
private MySharedpreference mySharedpreference = null;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_sliding_menu, container,
false);
logoutView = view.findViewById(R.id.toolbox_logout);
logoutView.setOnClickListener(this);
sycnView = view.findViewById(R.id.toolbox_sycn);
sycnView.setOnClickListener(this);
ridingView = view.findViewById(R.id.toolbox_riding);
ridingView.setSelected(true);
ridingView.setOnClickListener(this);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment newContent = null;
switch (v.getId()) {
case R.id.toolbox_sycn:
newContent = new SycnActivityFragment();
sycnView.setSelected(true);
ridingView.setSelected(false);
break;
case R.id.toolbox_riding:
newContent = new RidingActivityFragment();
ridingView.setSelected(true);
sycnView.setSelected(false);
break;
default:
break;
}
if (newContent != null) {
switchFragment(newContent);
}
}
private void switchFragment(Fragment fragment) {
if (getActivity() == null) {
return;
}
MainActivity mainActivity = new MainActivity();
mainActivity.switchContent(fragment);
}
提前致谢!
答案 0 :(得分:0)
问题
您的问题在于:您正在尝试在片段中创建另一个活动实例:
private void switchFragment(Fragment fragment) {
if (getActivity() == null) {
return;
}
MainActivity mainActivity = new MainActivity();
mainActivity.switchContent(fragment);
}
<强>解决方案强>
而不是这样你应该在片段内部获取getActivity并在SlidingMenuFragment中编写这段代码:
private void switchFragment(Fragment fragment) {
if (getActivity() == null) {
return;
}
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.switchContent(fragment);
}
如果您使用听众通过片段与您的活动进行交流,那将是一个很好的做法