Implementing a slideview from tutorial. What am I doing wrong?


Last Updated:

  1. RED_

    RED_ Well-Known Member

    Hey guys,

    First off, here is the tutorial I followed: Android User Interface Design: Horizontal View Paging

    The actual slideview works fine, moving left and right works just like swiping between homescreens of a launcher. However there are some issues. Mainly that the onClicks of my TextViews are unresponsive and that my ImageView buttons on everypage other than my first one (the one you see when the app launches) gives me a force close error.

    Each one of my buttons has one TextView and one ImageView. On my first screen the ImageViews work but when clicking on a TextView nothing happens. On every other page the ImageView's give me a force close and again, the TextViews don't do anything.

    This is my Java file (I removed the onClicks because I have a lot of them:

    Code (Text):
    1. package com.android.history;
    2.  
    3. import android.os.Bundle;
    4. import android.os.Parcelable;
    5. import android.app.Activity;
    6. import android.app.AlertDialog;
    7. import android.content.Context;
    8. import android.content.DialogInterface;
    9. import android.content.Intent;
    10. import android.support.v4.view.PagerAdapter;
    11. import android.support.v4.view.ViewPager;
    12. import android.view.LayoutInflater;
    13. import android.view.Menu;
    14. import android.view.View;
    15.  
    16. public class MainActivity extends Activity {
    17.  
    18.     @Override
    19.     public void onCreate(Bundle savedInstanceState) {
    20.         super.onCreate(savedInstanceState);
    21.         super.setContentView(R.layout.blank_main);
    22.        
    23.         MyPagerAdapter adapter = new MyPagerAdapter();
    24.         ViewPager myPager = (ViewPager) findViewById(R.id.mysixpanelpager);
    25.         myPager.setAdapter(adapter);
    26.         myPager.setCurrentItem(1);
    27.        
    28.         new EULA(this).show();
    29.     }
    30.  
    31.         [B]public void AboutUs (View v) {
    32.        
    33.         Intent gotoabout = new Intent(getApplicationContext(), AboutUsMain.class);
    34.         startActivity(gotoabout);      
    35.     }[/B]
    36.    
    37. //--- SLIDE VIEW HERE ---
    38.     private class MyPagerAdapter extends PagerAdapter {
    39.         public int getCount() {
    40.             return 6;
    41.         }
    42.         public Object instantiateItem(View collection, int position) {
    43.             LayoutInflater inflater = (LayoutInflater) collection.getContext()
    44.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    45.             int resId = 0;
    46.             switch (position) {
    47.             case 0:
    48.                 resId = R.layout.about;
    49.                 break;
    50.             case 1:
    51.                 resId = R.layout.main;
    52.                 break;
    53.             case 2:
    54.                 resId = R.layout.main_two;
    55.                 break;
    56.             case 3:
    57.                 resId = R.layout.main_three;
    58.                 break;
    59.             case 4:
    60.                 resId = R.layout.main_four;
    61.                 break;
    62.             case 5:
    63.                 resId = R.layout.main_five;
    64.                 break;
    65.             }
    66.             View view = inflater.inflate(resId, null);
    67.             ((ViewPager) collection).addView(view, 0);
    68.             return view;
    69.         }
    70.         @Override
    71.         public void destroyItem(View arg0, int arg1, Object arg2) {
    72.             ((ViewPager) arg0).removeView((View) arg2);
    73.         }
    74.         @Override
    75.         public boolean isViewFromObject(View arg0, Object arg1) {
    76.             return arg0 == ((View) arg1);
    77.         }
    78.         @Override
    79.         public Parcelable saveState() {
    80.             return null;
    81.         }
    82. }
    83.  
    84.     @Override
    85.     public boolean onCreateOptionsMenu(Menu menu) {
    86.         getMenuInflater().inflate(R.menu.main, menu);
    87.         return true;
    88.     }
    89.  
    90. }
    "blank_main" consists of a linear layout with the following inside it:
    Code (Text):
    1. <android.support.v4.view.ViewPager
    2.     android:layout_width="match_parent"
    3.     android:layout_height="match_parent"
    4.     android:id="@+id/mysixpanelpager"/>


    Is there a better way to do this if my problem can't be resolved?

    Really appreciate any help.

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Where are you setting your onClick listeners? Also, where is the functionality for your buttons?
  3. RED_

    RED_ Well-Known Member

    I'm setting my onClicks in the XML. android : onClick and all that. I've added one of my onClicks back to my java code, there are quite a few of those all leading to different pages. I'll put it in bold. It's under my onCreate method. It's in the same place in my code and as I said, there are quite a few of them.
  4. RED_

    RED_ Well-Known Member

    I think I found the issue here so if anyone has the same problem..

    I'm supposed to use a FragmentPagerAdapter not just a View pager adapter. Using fragments means that when I swipe between layouts the Activity will change too. This is what I need.

    The reason I'm having problems is because the viewpager is all in one activity so even though I slide to a new layout it doesn't change to that layouts Activity, it stays on MainActivity, therefore it can't find any onClicks or anything related to said activity.

Share This Page