Prem Prakash Prem Prakash - 1 month ago 8
Android Question

slide of image using viewpager

I need to make small app that will work like picture gallery: I should use view pager and when user swipe left or right, app should load image from url. I have an array with urls and it should start loading particular image when user swipe to new page (it should not load all images on the start). For example, if I'm on first page, I should see the first image from url (index 0 in array). When I swipe to second page, app should start loading image from index 1, and when if finish loading, it should appear on the screen.

During running my app i am geeting

11-06 07:47:43.108 9133-9133/com.example.prem.myapplication E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.prem.myapplication, PID: 9133
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference
at com.example.prem.myapplication.ImageAdapter.instantiateItem(ImageAdapter.java:42)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1091)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


Main Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ArrayList<String> articleList = getArticleData();

ImageAdapter adapter = new ImageAdapter(this,articleList);
viewPager.setAdapter(adapter);
}

private ArrayList<String> getArticleData() {
ArrayList<String> articleList = new ArrayList<String>(Arrays.asList(MyArticleData.articles));


return articleList;
}


}

MyArticleData for Urls

public class MyArticleData {

static String[] articles = {

"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png"
};
}


ImageAdapter

public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> data = new ArrayList<>();
public LayoutInflater mLayoutInflater;
View imageView;

ImageAdapter(Context context, ArrayList<String> urls) {
this.context = context;
this.data = urls;
}
@Override
public int getCount() {
return 30;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override

public Object instantiateItem(ViewGroup container, int position) {
ImageView
imageView = (ImageView) mLayoutInflater.inflate(R.layout.pager_item, container, false);

//ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
// ImageView imageView = new ImageView(context);
// int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
// imageView.setPadding(padding, padding, padding, padding);
// imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//imageView.setImageResource(GalImages[position]);

Picasso.with(context).load(data.get(position)).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}


}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>



pager_item.xml

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />





I am new to android.please help
Thanks

Answer
try this



public class MyArticleData {

    static String[] articles = {

            "https://api.learn2crack.com/android/images/donut.png",
            "https://api.learn2crack.com/android/images/eclair.png",
            "https://api.learn2crack.com/android/images/froyo.png",
            "https://api.learn2crack.com/android/images/ginger.png",
            "https://api.learn2crack.com/android/images/honey.png",
            "https://api.learn2crack.com/android/images/icecream.png",
            "https://api.learn2crack.com/android/images/jellybean.png",
            "https://api.learn2crack.com/android/images/kitkat.png",
            "https://api.learn2crack.com/android/images/lollipop.png",
            "https://api.learn2crack.com/android/images/marshmallow.png",
            "https://api.learn2crack.com/android/images/donut.png",
            "https://api.learn2crack.com/android/images/eclair.png",
            "https://api.learn2crack.com/android/images/froyo.png",
            "https://api.learn2crack.com/android/images/ginger.png",
            "https://api.learn2crack.com/android/images/honey.png",
            "https://api.learn2crack.com/android/images/icecream.png",
            "https://api.learn2crack.com/android/images/jellybean.png",
            "https://api.learn2crack.com/android/images/kitkat.png",
            "https://api.learn2crack.com/android/images/lollipop.png",
            "https://api.learn2crack.com/android/images/marshmallow.png",
            "https://api.learn2crack.com/android/images/donut.png",
            "https://api.learn2crack.com/android/images/eclair.png",
            "https://api.learn2crack.com/android/images/froyo.png",
            "https://api.learn2crack.com/android/images/ginger.png",
            "https://api.learn2crack.com/android/images/honey.png",
            "https://api.learn2crack.com/android/images/icecream.png",
            "https://api.learn2crack.com/android/images/jellybean.png",
            "https://api.learn2crack.com/android/images/kitkat.png",
            "https://api.learn2crack.com/android/images/lollipop.png",
            "https://api.learn2crack.com/android/images/marshmallow.png"
    };
}



public class MainActivity extends AppCompatActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ArrayList<String> articleList = getArticleData();

        ImageAdapter adapter = new ImageAdapter(this,articleList);
        viewPager.setAdapter(adapter);
    }

    private ArrayList<String> getArticleData() {
       ArrayList<String> articleList = new ArrayList<String>(Arrays.asList(MyArticleData.articles));


        return articleList;
    }
}







public class ImageAdapter extends PagerAdapter {
    Context context;
    ArrayList<String> data = new ArrayList<String>;

    ImageAdapter(Context context, ArrayList<String> urls) {
        this.context = context;
        this.data = urls;
    }
    @Override
    public int getCount() {
        return 30;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        //imageView.setImageResource(GalImages[position]);

        Picasso.with(context).load(data.get(position)).into(imageView);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }

}


add this dependency in your build gradle.

compile 'com.squareup.picasso:picasso:2.3.2'