2014-01-15 70 views
0

因此,我正在使用片段,因爲我想要在應用程序中禁用滑動模式,我嘗試使用CustomViewPager,如下所示:how to disable viewpager adapter on touching specific views?和此處:how to disable ViewPager swiping using android-support-v4.jar in android。 試圖遵循這一點,但沒有運氣。我試圖實現的是在按鈕上單擊應用程序不會跟隨碎片之間的波動手勢。就目前而言,即使我在啓動時禁用滑動操作,也沒關係。設置自己的ViewPager錯誤

二手片段:

package com.receive.bluereceive; 

public class Main extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener { 

private AppSectionsPagerAdapter mAppSectionsPagerAdapter; 
private CustomViewPager mViewPager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    final ActionBar actionBar = getActionBar(); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());   


    mViewPager = (CustomViewPager) findViewById(R.id.pager); 

    mViewPager.setAdapter(mAppSectionsPagerAdapter); 

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      // When swiping between different app sections, select the corresponding tab. 
      // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
      // Tab. 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 


    for (int i = 0; i < mAppSectionsPagerAdapter.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 
     // listener for when this tab is selected. 
     actionBar.addTab(
       actionBar.newTab() 
         .setText(mAppSectionsPagerAdapter.getPageTitle(i)) 
         .setTabListener(this)); 
    } 
} 

public class CustomViewPager extends ViewPager{ 

    public CustomViewPager(Context context){ 
     super(context); 
    } 

    public CustomViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev){ 
     return false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev){ 
     return false; 
    } 
} 
} 

這裏是CustomViewPager的.XML:

<?xml version="1.0" encoding="utf-8"?> 
<com.receive.bluereceive.CustomViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" /> 

最後,錯誤我得到:

01-15 03:06:14.482: E/AndroidRuntime(29489): FATAL EXCEPTION: main 
01-15 03:06:14.482: E/AndroidRuntime(29489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.receive.bluereceive/com.receive.bluereceive.Main}: android.view.InflateException: Binary XML file line #2: Error inflating class com.receive.bluereceive.CustomViewPager 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread.access$600(ActivityThread.java:153) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.os.Looper.loop(Looper.java:137) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread.main(ActivityThread.java:5227) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at java.lang.reflect.Method.invokeNative(Native Method) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at java.lang.reflect.Method.invoke(Method.java:511) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at dalvik.system.NativeStart.main(Native Method) 
01-15 03:06:14.482: E/AndroidRuntime(29489): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.receive.bluereceive.CustomViewPager 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:323) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.Activity.setContentView(Activity.java:1881) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at com.receive.bluereceive.Main.onCreate(Main.java:101) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.Activity.performCreate(Activity.java:5104) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262) 
01-15 03:06:14.482: E/AndroidRuntime(29489): ... 11 more 
01-15 03:06:14.482: E/AndroidRuntime(29489): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.receive.bluereceive.CustomViewPager" on path: /data/app/com.receive.bluereceive-2.apk 
01-15 03:06:14.482: E/AndroidRuntime(29489): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
01-15 03:06:14.482: E/AndroidRuntime(29489): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
01-15 03:06:14.482: E/AndroidRuntime(29489): ... 20 more 

是什麼原因造成的麻煩?我不知道我在這裏做錯了什麼。如果我以任何方式啓動應用程序,會很高興......

回答

0

問題是充氣機無法找到您的班級。請在自己的文件中聲明CustomViewPager類,並使用其正確的包路徑在xml中實例化它。