向后导航时FragmentView的碎片活动崩溃

时间:2015-09-24 13:11:37

标签: android fragment swipe

在此示例中,我可以通过滑动在3个页面之间导航。第一页(page0.xml)包含一个片段(@ + id / fragment1)。 向前滑动时工作正常,但是 问题是当从page2导航回page1时,应用程序崩溃。

这是stacktrace

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #14 Error inflating class fragment

如何解决这个问题?

package crash;
import com.example.crash.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;  

  public class Main extends FragmentActivity{

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    setContentView(R.layout.main);
    FragmentManager fm = getSupportFragmentManager();
    ViewPager vp = (ViewPager) findViewById(R.id.my_viewpager);
    MyAdapter ad = new MyAdapter(fm);
    vp.setAdapter(ad);

    super.onCreate(arg0);
}

public static class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {

        super(fm);
        // TODO Auto-generated constructor stub
        // this.myContext=myContext;
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Bundle bu = new Bundle();
        bu.putInt("page", arg0);
        Fragment myFragment = new MyFragment();
        myFragment.setArguments(bu);
        return myFragment;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }
}

public static class MyFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

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

        // TODO Auto-generated method stub
        View v;
        Bundle bu = getArguments();
        switch (bu.getInt("page")) {
        case 0:
            return inflater.inflate(R.layout.page0, container, false);
        case 1:
            return inflater.inflate(R.layout.page1, container, false);
        case 2:
            return inflater.inflate(R.layout.page2, container, false);
        default:
            return null;
        }

    }

    public static class MyIncludedFragment extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

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

        }
    }
}

}

main.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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/my_viewpager"
        >
 </android.support.v4.view.ViewPager>   

</LinearLayout>

page0.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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Page 0"
    android:textSize="48dp" />

<fragment
    android:id="@+id/fragment1"
    android:name="crash.Main$MyFragment$MyIncludedFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

page1.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Page 1"
        android:textSize="48dp" />

</LinearLayout>

page2.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Page 2"
        android:textSize="48dp" />

</LinearLayout>

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:background="#0ff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My fragment"
        android:textSize="48dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

将MyAdapter替换为:

public class FragmentAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public FragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

此外,使用newInstance()而不是调用默认构造函数总是一个好主意。请参阅此SO链接:Best practice for instantiating a new Android Fragment

完成后,使用以下内容将适配器设置为viewpager:

FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
fragmentAdapter.addFragment(MyFragment.newInstance(0), "Title 1");
fragmentAdapter.addFragment(MyFragment.newInstance(1), "Title 2");
fragmentAdapter.addFragment(MyFragment.newInstance(2), "Title 3");
viewPager.setAdapter(mFragmentAdapter);

编辑:要在您的案例中定义newInstance()方法,您需要执行以下操作:

public static class MyFragment extends Fragment {
    public static MyFragment newInstance(int arg0) {
         Bundle bu = new Bundle();
         bu.putInt("page", arg0);
         Fragment myFragment = new MyFragment();
         myFragment.setArguments(bu);
         return myFragment;
    }
    // rest of your code remains the same
}