Sky's On Ur Head Sky's On Ur Head - 4 months ago 28
Android Question

ImageSlider in android showing error

The image slider works fine until i do not load images in the array. That is:

int imageId[]={


};


But as soon as i do this:

int imageId[]={
R.drawable.mi

};


Let us that I have an activity A which has an image. Now, If i Long press on that image,Another Activity B should start. But the android app closes abruptly.

Here is my Activity code:

package com.example.hotel_app_regularuser;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class PageTwo extends Activity {
ViewPager vpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_two);
vpager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new customAdapter(PageTwo.this);
vpager.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page_two, menu);
return true;
}

}


class customAdapter extends PagerAdapter {
Context context;
int imageId[] = {
R.drawable.mi

};
public customAdapter(Context context) {
this.context = context;
}



public Object InstantiateItem(ViewGroup container, int item) {

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View viewitem = inflater.inflate(R.layout.activity_page_two, container, false);
ImageView im = (ImageView) viewitem.findViewById(R.id.imageView1);
im.setImageResource(imageId[item]);
TextView textView1 = (TextView) viewitem.findViewById(R.id.textView1);
textView1.setText("hi");
((ViewPager) container).addView(viewitem);

return viewitem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}

}

Answer

Change :

public Object InstantiateItem(ViewGroup container, int item) {

to :

@Override
public Object instantiateItem(ViewGroup container, int position) {

in your code and it will solve the problem.

i.e, add @Override and change the method name to instantiateItem from InstantiateItem

Comments