0

我正在尝试制作动态图片(使用您的手指)+ ViewPagerIndicator(点)

问题是我不想开始。他不适合这一行的布局是 17

android:name=".PlaceSlideFragment"

PlaceDetailsFragment

public class PlaceDetailsFragment extends SherlockFragment {
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public static final String TAG = "detailsFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main,
                container, false);

        mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

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

        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator) mIndicator).setSnap(true);

        mIndicator
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Toast.makeText(PlaceDetailsFragment.this.getActivity(),
                                "Changed to page " + position,
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageScrolled(int position,
                            float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
        return view;
    }

}

放置幻灯片片段

public final class PlaceSlideFragment extends Fragment {
    int imageResourceId;

    public PlaceSlideFragment(int i) {
        imageResourceId = i;
    }

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

    }

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

        ImageView image = new ImageView(getActivity());
        image.setImageResource(imageResourceId);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(null));

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }
}

PlaceSlidesFragmentAdapter

public class PlaceSlidesFragmentAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {

private int[] Images = new int[] { R.drawable.real_pixels, R.drawable.real_pixels,
    R.drawable.real_pixels, R.drawable.real_pixels

};

protected static final int[] ICONS = new int[] { R.drawable.ic_launcher,
    R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };

private int mCount = Images.length;

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

@Override
public Fragment getItem(int position) {
return new PlaceSlideFragment(Images[position]);
}

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

@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}

public void setCount(int count) {
if (count > 0 && count <= 10) {
    mCount = count;
    notifyDataSetChanged();
}
}
}

主要活动

public class MainActivity extends FragmentActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


}

主要(布局)

<?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/pager"
        android:layout_width="fill_parent"
        android:layout_height="200dp" />

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip" />

    <fragment   
            android:name=".PlaceSlideFragment"
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

日志猫

02-03 11:06:18.963: E/AndroidRuntime(10993): FATAL EXCEPTION: main
02-03 11:06:18.963: E/AndroidRuntime(10993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.os.Looper.loop(Looper.java:137)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at dalvik.system.NativeStart.main(Native Method)
02-03 11:06:18.963: E/AndroidRuntime(10993): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.Activity.setContentView(Activity.java:1881)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at com.test.MainActivity.onCreate(MainActivity.java:16)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.Activity.performCreate(Activity.java:5104)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-03 11:06:18.963: E/AndroidRuntime(10993):    ... 11 more
02-03 11:06:18.963: E/AndroidRuntime(10993): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment .PlaceSlideFragment: make sure class name exists, is public, and has an empty constructor that is public
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.support.v4.app.Fragment.instantiate(Fragment.java:359)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:262)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-03 11:06:18.963: E/AndroidRuntime(10993):    ... 21 more
02-03 11:06:18.963: E/AndroidRuntime(10993): Caused by: java.lang.ClassNotFoundException: Didn't find class ".PlaceSlideFragment" on path: /data/app/com.test-1.apk
02-03 11:06:18.963: E/AndroidRuntime(10993):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-03 11:06:18.963: E/AndroidRuntime(10993):    at android.support.v4.app.Fragment.instantiate(Fragment.java:381)
4

1 回答 1

0

在它前面加上包名

假设 PlaceSlideFragment 所在的包是 com.example 所以像这样改变它

<fragment   
            android:name="com.example.PlaceSlideFragment"
            android:id="@+id/fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

确保你有一个公共的空构造函数

public PlaceSlideFragment(){

}

编辑

如果您使用的是兼容性库中的Fragment ,请确保您的ActivityFragemntActivitySherlockSherlockFragmentActivity

于 2013-02-03T12:31:12.930 回答