我正在尝试使用ViewPager工作我的Fragment但是我收到了这个错误:
logcat的:
12-19 14:16:25.180: I/mFragment(5866): onCreate() LogicAnalizerActivity
12-19 14:16:25.235: I/mFragment(5866): FragmentAdapter Constructor
12-19 14:16:25.235: I/mFragment(5866): onResume() LogicAnalizerActivity
12-19 14:16:25.310: I/mFragment(5866): Page item 0
12-19 14:16:25.320: I/onCreate()(5866): onCreate LogicAnalizerView
12-19 14:16:25.350: D/AndroidRuntime(5866): Shutting down VM
12-19 14:16:25.355: W/dalvikvm(5866): threadid=1: thread exiting with uncaught exception (group=0x40d77300)
12-19 14:16:25.690: E/AndroidRuntime(5866): FATAL EXCEPTION: main
12-19 14:16:25.690: E/AndroidRuntime(5866): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.addView(ViewGroup.java:3249)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.addView(ViewGroup.java:3194)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.addView(ViewGroup.java:3170)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:874)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.View.measure(View.java:15172)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.View.measure(View.java:15172)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.View.measure(View.java:15172)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.View.measure(View.java:15172)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-19 14:16:25.690: E/AndroidRuntime(5866): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2432)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.View.measure(View.java:15172)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.os.Handler.handleCallback(Handler.java:615)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.os.Looper.loop(Looper.java:137)
12-19 14:16:25.690: E/AndroidRuntime(5866): at android.app.ActivityThread.main(ActivityThread.java:4978)
12-19 14:16:25.690: E/AndroidRuntime(5866): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 14:16:25.690: E/AndroidRuntime(5866): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 14:16:25.690: E/AndroidRuntime(5866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-19 14:16:25.690: E/AndroidRuntime(5866): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-19 14:16:25.690: E/AndroidRuntime(5866): at dalvik.system.NativeStart.main(Native Method)
ActivityFragment:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
public class LogicAnalizerActivity extends SherlockFragmentActivity {
private static final boolean DEBUG = true;
/** Numero de paginas del ViewPager */
private static final int pageCount = 1;
private static ActionBar actionBar;
private static ViewPager mViewPager;
private static MyFragmentAdapter mAdapter;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
if(DEBUG) Log.i("mFragment","onCreate() LogicAnalizerActivity");
actionBar = getSupportActionBar();
setContentView(R.layout.fragment);
mAdapter = new MyFragmentAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pagerLogicAnalizer);
mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setOffscreenPageLimit(1);
}
@Override
protected void onResume() {
if(DEBUG) Log.i("mFragment","onResume() LogicAnalizerActivity");
super.onResume();
}
/**
* Crea el ActionBar desde el XML actionbarlogic que define los iconos en el mismo
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(DEBUG) Log.i("onCreateOptionsMenu()", "onCreateOptionsMenu() -> LogicAnalizerView");
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.actionbarlogic, menu);
return true;
}
public static class MyFragmentAdapter extends FragmentPagerAdapter{
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
if(DEBUG) Log.i("mFragment","FragmentAdapter Constructor");
}
@Override
public Fragment getItem(int arg0) {
if(DEBUG) Log.i("mFragment","Page item " + arg0);
switch(arg0){
case 0:
return new LogicAnalizerFragment();
case 1:
return new LogicAnalizerFragment();
}
return new LogicAnalizerFragment();
}
@Override
public int getCount() {
return pageCount;
}
@Override
public void destroyItem(View collection, int position, Object view) {
if(DEBUG) Log.i("mFragment","Destroy item " + position);
((ViewPager) collection).removeView((LinearLayout) view);
}
}
}
LogicAnalizerFragment: 它太长了,你可以看到它here
fragment.xml之
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pagerLogicAnalizer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Fragment正常工作,但是当我尝试将它与ViewPager一起使用时,我收到了这个错误,我搜索了很多ViewPager示例,并且都是相同的。我添加了:
@Override
public void destroyItem(View collection, int position, Object view) {
if(DEBUG) Log.i("mFragment","Destroy item " + position);
((ViewPager) collection).removeView((LinearLayout) view);
}
试图解决问题,但它仍然存在。
答案 0 :(得分:1)
第250行,
return inflater.inflate(R.layout.logicanalizer, container);
添加false;
return inflater.inflate(R.layout.logicanalizer, container,false);
第3段确定天气它应该附在“容器”上,文件:
@ 3rd param attachToRoot是否应该将膨胀的层次结构附加到 根参数?如果为false,则root仅用于创建正确的 LayoutParams的子类,用于XML中的根视图。
@return膨胀层次结构的根视图。如果提供了root 而attachToRoot为true,这是root;否则它是根的 膨胀的XML文件。