2012-12-19 98 views
2

我試圖讓工作我與ViewPager片段,但我收到此錯誤:IllegalStateException異常 - removeView()在ViewPager

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> 

片段是正常工作,但是當我嘗試與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

怎樣的'LogicAnalizerFragment'片段樣子? – Luksprog

+0

這是太長,把它放在這裏,你可以在這裏看到它https://github.com/dragondgold/MultiWork/blob/master/src/com/multiwork/andres/LogicAnalizerFragment.java – Andres

+0

在'onCreateView'方法你應該有''return inflater.inflate(R.layout.logicanalizer,container,false);' – Luksprog

回答

1

250線,

return inflater.inflate(R.layout.logicanalizer, container); 

添加假;

return inflater.inflate(R.layout.logicanalizer, container,false); 

第三PARAMS determins天氣應該連接到 '容器' 與否,文檔:

@3rd param attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

@return The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

+0

使用return inflater.inflate(R.layout.logicanalizer,container,false);它部分工作! :D但是當我走到正確的時候一切都好,但是當我回去的時候,我得到了同樣的錯誤:/ – Andres

相關問題