2014-02-27 98 views
0

我想創建一個簡單的ViewPager與兩個片段。我正在使用兩個片段並將它們添加到viewPager以測試簡單的ViewPager。但我得到錯誤。請幫我Android的ViewPager與片段獲取錯誤

主類是:

public class MainActivity extends FragmentActivity { 
private MyPagerAdapter adapter; 
ViewPager viewpager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    adapter=new MyPagerAdapter(getSupportFragmentManager()); 
    viewpager=(ViewPager)findViewById(R.id.pager); 
    viewpager.setAdapter(adapter); 

} 

public class MyPagerAdapter extends FragmentPagerAdapter 
{ 
public MyPagerAdapter(android.support.v4.app.FragmentManager fm) { 
     super(fm); 

     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     //return fragments.size(); 
     return 2; 
    } 

    @Override 
    public boolean isViewFromObject(View arg0, Object arg1) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // TODO Auto-generated method stub 
     //return fragments.get(position); 
     switch (position) { 
     case 0: 
      //return new AboutUs(); 
      return new Programs(); 
     case 1: 
      return new fragment2Activity(); 
     } 
     return null; 
    } 
} 

}

FirstFragment是:

public class fragment2Activity extends Fragment { 

@Override 
public void onPause() { 
    // TODO Auto-generated method stub 

    super.onPause(); 
    onDestroyView(); 
} 
private String mContent = "???"; 
private static final String KEY_CONTENT = "TestFragment:Content"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    if ((savedInstanceState != null) 
      && savedInstanceState.containsKey(KEY_CONTENT)) { 
     mContent = savedInstanceState.getString(KEY_CONTENT); 
    } 

    } 
@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString(KEY_CONTENT, mContent); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 
    return inflater.inflate(R.layout.fragment2_layout, container, true); 

} 

}

SecondFragment是:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Programs extends Fragment 
{ 

@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 

    super.onDestroy(); 
} 

private String mContent = "???"; 
private static final String KEY_CONTENT = "TestFragment:Content"; 

@Override 
public void onPause() { 
    // TODO Auto-generated method stub 

    super.onPause(); 
    onDestroyView(); 
} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    if ((savedInstanceState != null) 
      && savedInstanceState.containsKey(KEY_CONTENT)) { 
     mContent = savedInstanceState.getString(KEY_CONTENT); 
    } 

    } 
@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString(KEY_CONTENT, mContent); 
} 




@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View inf = inflater.inflate(R.layout.fragment1_layout, container, false); 
    return inf; 
} 

}

我收到followinf錯誤:

02-27 10:54:05.157: E/AndroidRuntime(21364): FATAL EXCEPTION: main 
02-27 10:54:05.157: E/AndroidRuntime(21364): java.lang.IllegalStateException: The specified child  already has a parent. You must call removeView() on the child's parent first. 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.addViewInner(ViewGroup.java:3345) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.addView(ViewGroup.java:3216) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.addView(ViewGroup.java:3173) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.addView(ViewGroup.java:3153) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.View.measure(View.java:13011) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.View.measure(View.java:13011) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.View.measure(View.java:13011) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2162) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.View.measure(View.java:13011) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1091) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.os.Looper.loop(Looper.java:154) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at android.app.ActivityThread.main(ActivityThread.java:4945) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at java.lang.reflect.Method.invokeNative(Native Method) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at java.lang.reflect.Method.invoke(Method.java:511) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-27 10:54:05.157: E/AndroidRuntime(21364): at dalvik.system.NativeStart.main(Native Method) 

回答

1

試試這個,在MainActivity

Vector<Fragment> fragments = new Vector<Fragment>(); 
    fragments.add(Fragment.instantiate(this, Programs.class.getName())); 
    fragments.add(Fragment.instantiate(this, fragment2Activity.class.getName())); 
    adapter=new MyPagerAdapter(getSupportFragmentManager(),fragments); 

添加這和MyPagerAdapter

private List<Fragment> fragments; 
    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fragments = fragments; 
    } 


    @Override 
    public Fragment getItem(int position) 
    { 
     return this.fragments.get(position); 

    } 

進行修改和刪除isViewFromObject()

+0

thnx ..它的工作......你能解釋一下,我在做什麼錯誤:) –

1

有兩件事情:

  1. 在適配器

    ,而是執行此操作:

  2. 在你的onCreateView s,這樣做而不是:

    return inflater.inflate(R.layout.fragment2_layout, container, **false**); 
    
  3. ,如果它仍然無法正常工作,嘗試在適配器覆蓋destroyItem():

    @Override 
    
    public void destroyItem(ViewGroup container, int position, Object object) { 
    
        ((ViewPager) container).removeView((View) object); 
    
    } 
    
+0

thnankx ..錯誤沒了..但我不能看到屏幕上的任何東西..我gettign空白屏幕.. 這是我的activity_main.xml佈局。

+0

怪異你首先檢查一下,確保你的碎片在正常的非尋呼屏幕上顯示正常。 – josephus