• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

RED_

Well-Known Member
Nov 13, 2010
209
8
London
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:
package com.android.history;

import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.blank_main);
        
        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.mysixpanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);
        
        new EULA(this).show();
    }

        [B]public void AboutUs (View v) {
    	
    	Intent gotoabout = new Intent(getApplicationContext(), AboutUsMain.class);
    	startActivity(gotoabout);   	
    }[/B]
    
//--- SLIDE VIEW HERE ---
    private class MyPagerAdapter extends PagerAdapter {
        public int getCount() {
            return 6;
        }
        public Object instantiateItem(View collection, int position) {
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            int resId = 0;
            switch (position) {
            case 0:
            	resId = R.layout.about;
            	break;
            case 1:
            	resId = R.layout.main;
            	break;
            case 2:
                resId = R.layout.main_two;
                break;
            case 3:
                resId = R.layout.main_three;
                break;
            case 4:
                resId = R.layout.main_four;
                break;
            case 5:
                resId = R.layout.main_five;
                break;
            }
            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }
        @Override
        public Parcelable saveState() {
            return null;
        }
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

"blank_main" consists of a linear layout with the following inside it:
Code:
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mysixpanelpager"/>



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

Really appreciate any help.
 
Where are you setting your onClick listeners? Also, where is the functionality for your buttons?

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.
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones