我正在做什么 ::我正在尝试显示标签式viewpager
MainActivity.java
public class MainActivity extends FragmentActivity implements TabListener {
ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
viewPager=(ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Tab1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Tab2");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("Tab3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
@Override
protected void onDestroy() {
Log.d(getClass().getName(), "[onDestroy(activity)]");
super.onDestroy();
}
@Override
public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment=null;
if(arg0==1){
fragment=new SortRestaurantFragment();
}
if(arg0==2){
fragment=new SortRestaurantFragment();
}
if(arg0==3){
fragment=new SortRestaurantFragment();
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
main_activity.xml
<android.suppoort.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我还添加了支持库::
日志::
02-10 02:34:17.471: E/AndroidRuntime(641): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.findmybuffet.restaurant_module/com.findmybuffet.restaurant_module.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.os.Looper.loop(Looper.java:137)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-10 02:34:17.471: E/AndroidRuntime(641): at dalvik.system.NativeStart.main(Native Method)
02-10 02:34:17.471: E/AndroidRuntime(641): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Activity.setContentView(Activity.java:1835)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.findmybuffet.restaurant_module.MainActivity.onCreate(MainActivity.java:26)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Activity.performCreate(Activity.java:4465)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-10 02:34:17.471: E/AndroidRuntime(641): ... 11 more
02-10 02:34:17.471: E/AndroidRuntime(641): Caused by: java.lang.ClassNotFoundException: android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-10 02:34:17.471: E/AndroidRuntime(641): ... 20 more
02-10 02:34:17.471: E/AndroidRuntime(641): FATAL EXCEPTION: main
02-10 02:34:17.471: E/AndroidRuntime(641): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.findmybuffet.restaurant_module/com.findmybuffet.restaurant_module.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.os.Looper.loop(Looper.java:137)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-10 02:34:17.471: E/AndroidRuntime(641): at dalvik.system.NativeStart.main(Native Method)
02-10 02:34:17.471: E/AndroidRuntime(641): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Activity.setContentView(Activity.java:1835)
02-10 02:34:17.471: E/AndroidRuntime(641): at com.findmybuffet.restaurant_module.MainActivity.onCreate(MainActivity.java:26)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Activity.performCreate(Activity.java:4465)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-10 02:34:17.471: E/AndroidRuntime(641): ... 11 more
02-10 02:34:17.471: E/AndroidRuntime(641): Caused by: java.lang.ClassNotFoundException: android.suppoort.v4.view.ViewPager
02-10 02:34:17.471: E/AndroidRuntime(641): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-10 02:34:17.471: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
02-10 02:34:17.471: E/AndroidRuntime(641): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-10 02:34:17.471: E/AndroidRuntime(641): ... 20 more
main_activity.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
记录::
02-10 03:02:09.681: E/AndroidRuntime(878): FATAL EXCEPTION: main
02-10 03:02:09.681: E/AndroidRuntime(878): java.lang.NullPointerException
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.View.measure(View.java:12603)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.View.measure(View.java:12603)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.View.measure(View.java:12603)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
02-10 03:02:09.681: E/AndroidRuntime(878): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.View.measure(View.java:12603)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:137)
02-10 03:02:09.681: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-10 03:02:09.681: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 03:02:09.681: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 03:02:09.681: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-10 03:02:09.681: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-10 03:02:09.681: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:7)
在main_activity.xml
中,您的班级名称拼写错误。
android.suppoort.v4.view.ViewPager
应该是
android.support.v4.view.ViewPager