java.lang.IndexOutOfBoundsException:索引3无效,viewpager

时间:2016-02-03 07:44:28

标签: android android-viewpager

在我的应用程序中使用两种语言使用相同的适配器,首先如果用户选择第一语言,则数据填充在ViewPager中,如果我们在此之后滑动两页,三页,如果我们更改另一种语言数据填充在{{ 1}},但如果我们点击寻呼机中的任何按钮,它会显示此

ViewPager

任何人都可以提前解决此错误。

error java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2,

}

Execeptions

public class MyCustomAdapter extends PagerAdapter {


Context context1;
LayoutInflater inflater = null;
String title;
public static LinearLayout full_link, containers, language;
public static Uri bmpUri = null;
TextView txtfullstory;
int pStatus = 0;
private Handler handler = new Handler();
public static Bitmap bitmap;
private SharedPreferences.Editor editor;
private SharedPreferences pref;
String user_type_id;

public MyCustomAdapter(Context context1,
                       ArrayList<String> NewsidArray,
                       ArrayList<String> NewsdescArray,
                       ArrayList<String> NewstitleArray,
                       ArrayList<String> NewsimageArray,
                       ArrayList<String> NewsdateArray,
                       ArrayList<String> NewsauthorArray,
                       ArrayList<String> NewsurlArray) {

    this.context1 = context1;
    newsidArray = NewsidArray;
    newsdescArray = NewsdescArray;
    newstitleArray = NewstitleArray;
    newsimageArray = NewsimageArray;
    newsdateArray = NewsdateArray;
    newsauthorArray = NewsauthorArray;
    newsurlArray = NewsurlArray;

}

@Override
public int getCount() {

    if (newstitleArray == null) {
        return 0;
    }
    return newstitleArray.size();
}


@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}



public Object instantiateItem(ViewGroup container, final int position) {


    final int delPosition = position;
    final Holder holder     =   new Holder();

    inflater                =   (LayoutInflater) context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View itemView     =   inflater.inflate(R.layout.fragment_layout, container, false);

    holder.date             =   (TextView) itemView.findViewById(R.id.txtdate);
    holder.txttitle         =   (TextView) itemView.findViewById(R.id.txttitle);
    holder.txtdesc          =   (TextView) itemView.findViewById(R.id.txtdesc);
    holder.txtauthor        =   (TextView) itemView.findViewById(R.id.txtauthor);
    holder.imageview        =   (ImageView) itemView.findViewById(R.id.imageview);
    holder.share            =   (ImageView) itemView.findViewById(R.id.share);
    holder.url              =   (TextView) itemView.findViewById(R.id.url);
    holder.reverse          =   (ImageView) itemView.findViewById(R.id.reverse);
    holder.progress         =   (ProgressBar) itemView.findViewById(R.id.progress);
    full_link               =   (LinearLayout)itemView.findViewById(R.id.line_above);
    containers              =   (LinearLayout)itemView.findViewById(R.id.containers);
    language                =   (LinearLayout)itemView.findViewById(R.id.language);
    txtfullstory            =   (TextView) itemView.findViewById(R.id.txtfullstory);

    String date             =    newsdateArray.get(position).toString();
    String image            =    newsimageArray.get(position).toString();



    holder.txtdesc.setText(Html.fromHtml(newsdescArray.get(position).toString()));
    holder.txttitle.setText(Html.fromHtml(newstitleArray.get(position).toString()));
    holder.txtauthor.setText(Html.fromHtml(newsauthorArray.get(position).toString()));





    holder.url.setText(newsurlArray.get(position).toString());
    Typeface font           =   Typeface.createFromAsset(context1.getAssets(), "fonts/Seravek.ttc");
    holder.txtdesc.setTypeface(font);
    holder.txttitle.setTypeface(font, Typeface.BOLD);
    holder.txtauthor.setTypeface(font);
    title = newstitleArray.get(position);



    pref             = context1.getSharedPreferences("MyPref", 0);
    editor           = pref.edit();
    user_type_id = pref.getString("langid", null);

    if(user_type_id != null && user_type_id.equals("2")){
        txtfullstory.setText(Html.fromHtml("पर पूरी कहानी"));

    }else {
        txtfullstory.setText(Html.fromHtml("Full story on"));

    }

    try {
           @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sss");
            Date parsedDate = dateFormat1.parse(date);
          long times = parsedDate.getTime();
            new Timestamp(times);
            String result = (String) DateUtils.getRelativeTimeSpanString(times, System.currentTimeMillis() + 5 * 60 * 1000, 0);
            holder.date.setText(result);

        } catch (ParseException e) {
            Log.d("Error", e.getMessage());
        }


    if(!(!image.equalsIgnoreCase("") && !image.equalsIgnoreCase(null))) {
            holder.imageview.setVisibility(View.INVISIBLE);
            }
            else{

        holder.progress.setVisibility(View.VISIBLE);
        holder.imageview.setVisibility(View.INVISIBLE);


        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (pStatus <= 100) {

                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            holder.progress.setProgress(pStatus);
                            holder.progress.setSecondaryProgress(pStatus + 5);
                        }
                    });
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pStatus++;
                }
            }
        }).start();

        holder.imageview.setScaleType(ImageView.ScaleType.FIT_XY);
        Picasso.with(context1).load(WebService.WS_ImageOnlineURL + image).fit().centerCrop()
                .error(R.drawable.image_loading).transform(new RoundedTransformation(4, 0)).into(holder.imageview, new Callback()
        {
            @Override
            public void onSuccess()
            {

                holder.progress.setVisibility(View.INVISIBLE);
                holder.imageview.setVisibility(View.VISIBLE);
            }

            @Override
            public void onError()
            {

            }
        });
            }


    itemView.setDrawingCacheEnabled(true);
    itemView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    itemView.layout(0, 0, 300, 400);
    itemView.buildDrawingCache(true);



    holder.share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            holder.share.setVisibility(View.INVISIBLE);
            holder.reverse.setVisibility(View.VISIBLE);

            bitmap = Bitmap.createBitmap(itemView.getWidth(), itemView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            itemView.draw(canvas);
            try {
                // output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
                File file = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");

                // File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "share_image_" + System.currentTimeMillis() + ".png");

                file.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();

                bmpUri = Uri.fromFile(file);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            holder.share.setVisibility(View.VISIBLE);
            holder.reverse.setVisibility(View.INVISIBLE);

            try {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                sendIntent.putExtra(Intent.EXTRA_TEXT, newstitleArray.get(myCustomPosition) + "\r\n" + "Get odd news only on Oddcast" + "\r\n" + "https://goo.gl/8jeJga");
                sendIntent.setType("image/*");
                context1.startActivity(sendIntent);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    });


    holder.txttitle.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            holder.share.setVisibility(View.INVISIBLE);
            holder.reverse.setVisibility(View.VISIBLE);

            bitmap = Bitmap.createBitmap(itemView.getWidth(),itemView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            itemView.draw(canvas);
            try {
                // output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
                File file =  new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");

                // File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "share_image_" + System.currentTimeMillis() + ".png");

                file.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();

                bmpUri = Uri.fromFile(file);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            holder.share.setVisibility(View.VISIBLE);
            holder.reverse.setVisibility(View.INVISIBLE);



            Intent i = new Intent(context1, NewsDetailActivity.class);
            i.putExtra("url", "" + newsurlArray.get(myCustomPosition));
            i.putExtra("title", "" + newstitleArray.get(myCustomPosition));
            context1.startActivity(i);
        }
    });


    full_link.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            holder.share.setVisibility(View.INVISIBLE);
            holder.reverse.setVisibility(View.VISIBLE);

            bitmap = Bitmap.createBitmap(itemView.getWidth(),itemView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            itemView.draw(canvas);
            try {
                File file =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
                file.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();

                bmpUri = Uri.fromFile(file);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            holder.share.setVisibility(View.VISIBLE);
            holder.reverse.setVisibility(View.INVISIBLE);

            Intent i = new Intent(context1, NewsDetailActivity.class);
            i.putExtra("url", "" + newsurlArray.get(myCustomPosition));
            i.putExtra("title", "" +  newstitleArray.get(myCustomPosition));
            context1.startActivity(i);
        }
    });
    language.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.e("newstitleArray", String.valueOf(newstitleArray.size()));
            Intent language = new Intent(context1, UserPreferedLanguage.class);
            context1.startActivity(language);
            Activity activity = (Activity) context1;
            activity.overridePendingTransition(R.anim.left_in, R.anim.right_out);

        }
    });




    container.addView(itemView);
    return itemView;
}

public class Holder {
    TextView date,txtauthor,txttitle,txtdesc,url;   //Initialize TextView variable
    ImageView  imageview,share,reverse;
    ProgressBar progress;
}

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

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

0 个答案:

没有答案