0
嗯,我是新來的Android,我想做一個3頁的刷卡應用程序。我從eclipse創建了一個新項目,並使用固定選項卡和可滾動頁面選擇了項目。我刪除了這些標籤,因爲我只想要一個應用程序與可滾動頁面。下面的代碼android片段3頁
在MainActivity類
@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int PAGE_COUNT=3;
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(3);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0:
fragment = Fragment.instantiate(getBaseContext(), first.class.getName());
break;
case 1:
fragment = Fragment.instantiate(getBaseContext(), second.class.getName());
break;
case 2:
fragment = Fragment.instantiate(getBaseContext(), MainActivity.class.getName());
break; }
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
public class good extends Fragment{
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
這是一個片段first.java
public class first extends FragmentActivity{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// here you can load whatever layout you want for your fragment
View v = inflater.inflate(R.layout.prva, container, false);
return v;
}}
錯誤我:
10-30 14:46:20.355: E/AndroidRuntime(7828): android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.swipe.MainActivity$SectionsPagerAdapter$good: make sure class name exists, is public, and has an empty constructor that is public
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
10-30 14:46:20.355: E/AndroidRuntime(7828): at com.example.swipe.MainActivity$SectionsPagerAdapter.getItem(MainActivity.java:91)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.view.View.measure(View.java:15473)
10-30 14:46:20.355: E/AndroidRuntime(7828): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5056)
http://s15.postimg.org/6nqjnqu0r/eclipseee。 png –
好!!! 好吧,這個問題是退後一步...你必須將android-suport-v4.jar庫添加到構建路徑中,轉到屏幕截圖中的前一個選項卡「Libraries」,然後單擊添加並搜索項目根目錄名爲lib或libs的文件夾放入此文件夾中,您必須找到android-support-v4.jar,單擊它並添加到構建路徑。 下一步你可以做我在之前的評論中解釋過的。 –