在我的项目中,我使用 viewpager 和三个名为History
, Main
,的选项卡Map
。主要活动包括计时器、秒表等。地图显示谷歌地图。现在进入历史,我只使用简单的 Textview。
Viewpager 流向:历史 -主要-地图
我将 Main 设置为当前项目(默认 Main 标签显示)。现在,当我从Main > Map和Map > Main滑动时,它的工作完美。但是当从Main > History滑动时 没有错误,但从History返回Main ( History > Main ) eclipse 给我错误,例如:
E/AndroidRuntime( 533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
仅供参考,我正在使用intent into Map Activity
。请检查下面的代码。请让我知道如何解决我的问题。
ViewPager 类:
@Override
public Fragment getItem(int position) {
// return SwipeyTabFragment.newInstance(TITLES[position]);
Fragment f = new Fragment();
switch (position) {
case 0:
f = History.newInstance(position);
break;
case 1:
f = Main.newInstance(position);
break;
case 2:
f = Map.newInstance(position);
break;
}
return f;
}
历史课:
public class History extends Fragment {
public static Fragment newInstance(int position) {
History f = new History();
Bundle args = new Bundle();
args.putInt("title", position);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.history, null);
( (TextView) root.findViewById(R.id.text)).setText("Hello");
return root;
}
}
主类:
public class Main extends Fragment implements GPSCallback {
......
public static Fragment newInstance(int position) {
Main f = new Main();
Bundle args = new Bundle();
args.putInt("title", position);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.main, null);
.......
return root;
}
........
}
地图类:
public class Map extends Fragment {
private static final String KEY_STATE_BUNDLE = "localActivityManagerState";
private LocalActivityManager mLocalActivityManager;
protected LocalActivityManager getLocalActivityManager() {
return mLocalActivityManager;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle state = null;
if (savedInstanceState != null) {
state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
}
mLocalActivityManager = new LocalActivityManager(getActivity(), true);
mLocalActivityManager.dispatchCreate(state);
}
public static Fragment newInstance(int position) {
Map f = new Map();
Bundle args = new Bundle();
args.putInt("title", position);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(getActivity(), MapViewActivity.class);
// Intent i = new Intent(getActivity(), hellogooglemap.class);
Window w = mLocalActivityManager.startActivity("tag", i);
View currentView=w.getDecorView();
currentView.setVisibility(View.VISIBLE);
currentView.setFocusableInTouchMode(true);
((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
return currentView;
/*ViewGroup root = (ViewGroup) inflater.inflate(R.layout.history, null);
( (TextView) root.findViewById(R.id.text)).setText("Hello");
return root;*/
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(KEY_STATE_BUNDLE,
mLocalActivityManager.saveInstanceState());
}
@Override
public void onResume() {
super.onResume();
mLocalActivityManager.dispatchResume();
}
@Override
public void onPause() {
super.onPause();
mLocalActivityManager.dispatchPause(getActivity().isFinishing());
}
@Override
public void onStop() {
super.onStop();
mLocalActivityManager.dispatchStop();
}
@Override
public void onDestroy() {
super.onDestroy();
mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
}
}
日志猫:
E/AndroidRuntime( 533): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime( 533): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
E/AndroidRuntime( 533): at android.view.ViewGroup.addView(ViewGroup.java:1871)
E/AndroidRuntime( 533): at android.view.ViewGroup.addView(ViewGroup.java:1828)
E/AndroidRuntime( 533): at android.view.ViewGroup.addView(ViewGroup.java:1808)
E/AndroidRuntime( 533): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
E/AndroidRuntime( 533): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:874)
E/AndroidRuntime( 533): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1240)
E/AndroidRuntime( 533): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:612)
E/AndroidRuntime( 533): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
E/AndroidRuntime( 533): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
E/AndroidRuntime( 533): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
E/AndroidRuntime( 533): at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
E/AndroidRuntime( 533): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
E/AndroidRuntime( 533): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
E/AndroidRuntime( 533): at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
E/AndroidRuntime( 533): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 533): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
E/AndroidRuntime( 533): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 533): at android.view.View.draw(View.java:6883)
E/AndroidRuntime( 533): at android.widget.FrameLayout.draw(FrameLayout.java:357)
E/AndroidRuntime( 533): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
E/AndroidRuntime( 533): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 533): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
E/AndroidRuntime( 533): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 533): at android.view.View.draw(View.java:6883)
E/AndroidRuntime( 533): at android.widget.FrameLayout.draw(FrameLayout.java:357)
E/AndroidRuntime( 533): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
E/AndroidRuntime( 533): at android.view.ViewRoot.draw(ViewRoot.java:1522)
E/AndroidRuntime( 533): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
E/AndroidRuntime( 533): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
E/AndroidRuntime( 533): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 533): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 533): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 533): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 533): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 533): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 68): Force finishing activity com.android.gps/.viewpager.SwipeyTabsSampleActivity
W/ActivityManager( 68): Activity pause timeout for HistoryRecord{40716740 com.android.gps/.viewpager.SwipeyTabsSampleActivity}
谢谢。
编辑:
没有意图进入地图活动它可以工作 pwrfect。我的意思是当我使用与历史相同的地图时,没有任何错误。