Viewpager与片段问题

时间:2013-08-28 21:25:48

标签: android android-fragments android-viewpager

我使用了此link

中显示的示例

它在纵向位置运行良好,但当我将手机转到横向位置时,它会强行关闭,

任何帮助克服这个问题的人都将不胜感激,谢谢。

ViewPagerExample

public class ViewPagerExample extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
}

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new DetailFragment();
        case 1:
            return new ImageFragment(R.drawable.ic_launcher);
        case 2:
            return new ImageFragment(R.drawable.five);

        default:
            return null;
        }
    }
}
}

ImageFragment

public class ImageFragment extends Fragment {
private final int imageResourceId;

public ImageFragment(int imageResourceId) {
    this.imageResourceId = imageResourceId;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("Test", "hello");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.image_layout, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
    imageView.setImageResource(imageResourceId);
    return view;
}
}

DetailFragment

public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("Test", "hello");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.details, container, false);
    TextView textView = (TextView) view.findViewById(R.id.detailsText);
    textView.setText("Testing");
    return view;
}
}

logcat的

java.lang.RuntimeException: Unable to start activity   
ComponentInfo{com.test.demo/com.test.demo.ViewPagerExample}:
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate   
fragment com.test.demo.ImageFragment:
make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2836)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to  
instantiate fragment com.test.demo.ImageFragment: 
make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at         
  android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1783)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at com.test.demo.ViewPagerExample.onCreate(ViewPagerExample.java:17)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 12 more
Caused by: java.lang.InstantiationException: com.test.demo.ImageFragment
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
... 18 more

1 个答案:

答案 0 :(得分:0)

片段必须有一个空构造函数。您希望传递给片段的所有内容都使用

完成
@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    Bundle args = new Bundle();
    switch (position) {
    case 0:
        fragment = new DetailFragment();
        break;
    case 1:
        args.putInt("image",R.drawable.ic_launcher);
        fragment = new ImageFragment();
        fragment.setArguments(args);
        break;
    case 2:
        args.putInt("image",R.drawable.five);         
        fragment = new ImageFragment();
        fragment.setArguments(args);
        break;
    }
    return fragment;
}

然后在片段中:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
     int imageID = getArguments().getInt("image");
}