带有碎片的Android ViewPager收到错误

时间:2014-02-27 06:05:32

标签: android android-fragments android-viewpager

我正在尝试创建一个包含两个片段的简单ViewPager。我使用两个片段并将它们添加到viewPager来测试简单的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)

3 个答案:

答案 0 :(得分:1)

一些事情:

    在您的适配器中
  1. ,请改为:

    @Override
    
    public boolean isViewFromObject(View arg0, Object arg1) {
    
      return **arg0==arg1**;
    
    }
    
  2. 在您的onCreateView中,请改为:

    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);
    
    }
    

答案 1 :(得分: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()

答案 2 :(得分:0)

请参阅以下链接,您可以获得具有适当布局的简单示例。 How to implement a ViewPager with different Fragments / Layouts