java.lang.IndexOutOfBoundsException:无效索引 7,大小为 7

2024-05-01

我正在尝试实现视图寻呼机,在我的视图寻呼机图像来自服务器,我能够显示和滚动,但当我到达最后一个图像时,它显示错误并且应用程序崩溃,以下是我的代码片段

   public class Test_Pager extends Activity{


private String strtd;
String[] imgStr;
ImageView imageView;
ArrayList<String> userImgArrayList;
String[] myURLs;


/*country list*/
JSONArray country_list=null;
private ServiceHandler sh;
private String jsonStr;
private JSONObject jsonObj;
private String user_img;
private String user_pro;
private static String PROFILE_VIEW_URL = "";

private static final String USER_IMG="product_images";


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_pagerss);

    userImgArrayList = getIntent().getStringArrayListExtra("user_images");
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
       ImageAdapter adapter = new ImageAdapter(this);
       viewPager.setAdapter(adapter);

     imageView = (ImageView) findViewById(R.id.full_image_view);


     PROFILE_VIEW_URL="";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    sh = new ServiceHandler();

        // Making a request to url and getting response
        jsonStr = sh.makeServiceCall(PROFILE_VIEW_URL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

     try {
         jsonObj = new JSONObject(jsonStr);


             /*proview_business = jsonObj.getString(PROFILE_VIEW_BUSINESS);
             proview_subcat = jsonObj.getString(PROFILE_VIEW_SUB_CATAGORY);
             proview_mainpro = jsonObj.getString(PROFILE_VIEW_MAINPRODUCTS);
             proview_expr = jsonObj.getString(PROFILE_VIEW_EXPERIENCE);
             proview_cmpname = jsonObj.getString(PROFILE_VIEW_COMPANYNAME);
             proview_website = jsonObj.getString(PROFILE_VIEW_WEBSITE);*/

            // user_img=jsonObj.getString(USER_IMG);



            user_img=jsonObj.getString(USER_IMG);
            user_img = "";
            userImgArrayList = new ArrayList<String>();//declare userImgArrayList globally like ArrayList<String> userImgArrayList;
            JSONArray picarray = jsonObj.getJSONArray(USER_IMG);
            for(int i=0;i< picarray.length();i++)
            {
                user_img = picarray.getString(i);
                userImgArrayList.add(user_img);
                Log.d("mylog", "curent  pro pic  = " + user_img);
            }


     } catch (JSONException e) {
         e.printStackTrace();
     }

}
public class ImageAdapter extends PagerAdapter {
     Context context;
      ImageAdapter(Context context)
     {
     this.context=context;
     }
      @Override
      public int getCount() {
      return USER_IMG.length();
      }

      @Override
        public void destroyItem(View container, int position, Object object) {
             ((ViewPager) container).removeView((View) object);
        }
     @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.activity_horizontal_margin);
            imageView.setPadding(padding, padding, padding, padding);
            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //Picasso.with(context).load(userImgArrayList.get(position)).into(imageView);
            Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);

            /*for(int i=0; i<myURLs.length;i++)
            { 

                try {
                    url = new URL(myURLs[i]);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 imageView.setImageBitmap(bmp);
            }
            */
            // imageView.setImageURI(Uri.parse(imgStr[position]));
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }
     }

  }

Myxml

        <RelativeLayout 
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:background="@android:color/white"
         xmlns:android="http://schemas.android.com/apk/res/android">







       <ScrollView 
           android:layout_height="match_parent"
           android:layout_width="match_parent"
            android:layout_below="@+id/lnr"
            android:fillViewport="true"

                >

           <LinearLayout 
          android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >

            <LinearLayout 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
            android:orientation="vertical"
                 >
          <android.support.v4.view.ViewPager
      android:id="@+id/view_pager"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

<ImageView android:id="@+id/full_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/></LinearLayout>

 <LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/secondpart"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    >
      <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:shrinkColumns="*"  android:stretchColumns="*" >
       <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:gravity="center_horizontal">

        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Product Name"
            android:layout_marginLeft="5dp"
            android:textColor="@android:color/black"
            />


        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/txtageprofile"

            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            /></TableRow>
        <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:gravity="center_horizontal"
                        android:paddingTop="5dp"
                        >
         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Code"
            android:layout_marginLeft="5dp"
            android:textColor="@android:color/black"
            />


        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"

            android:id="@+id/txtheightprofile"

          android:textColor="@android:color/black"
          android:layout_marginLeft="5dp"
            /></TableRow>
         <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:gravity="center_horizontal"
                        android:paddingTop="5dp"
                        >
         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Purchase Rate"
            android:layout_marginLeft="5dp"
            android:textColor="@android:color/black"
            />


        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"

            android:id="@+id/txtmrgprofile"

            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            /></TableRow>
          <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:gravity="center_horizontal"
                        android:paddingTop="5dp">
         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Weight"
            android:layout_marginLeft="5dp"
            android:textColor="@android:color/black"
            />

         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"

            android:id="@+id/txtworkingprofile"

            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            /></TableRow>
           <TableRow
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:gravity="center_horizontal"
                        android:paddingTop="5dp"
                        >
         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Sales Rate"
            android:layout_marginLeft="5dp"
            android:textColor="@android:color/black"
            />

         <TextView 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/txtplaceprofile"
            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            /></TableRow>
            </TableLayout>
            </LinearLayout>
           <LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/secondpart"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    >
            <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical|right"

            android:id="@+id/txteatprofile"
            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
           android:layout_marginRight="5dp"
            />
            </LinearLayout>


      </LinearLayout>
      </ScrollView>
      </RelativeLayout>

USER_IMG.length();返回的长度String(“product_images”的字符数)。您应该返回数据集的大小。改变

 @Override
 public int getCount() {
      return USER_IMG.length();
 }

to

 @Override
 public int getCount() {
      if (userImgArrayList == null) {
         return 0;
      } 
      return userImgArrayList.size();
 }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.IndexOutOfBoundsException:无效索引 7,大小为 7 的相关文章

  • 如何挂载加密的APK扩展文件?

    我尝试以这种方式挂载扩展文件 final StorageManager storageManager StorageManager getSystemService STORAGE SERVICE String obbPath Enviro
  • 文件递归搜索

    我试图在根目录及其子目录中查找文件 步骤1 在指定路径中查找目录 步骤2 如果找到上述目录 则在其子目录之一中查找文件 为此 我使用下面的代码片段进行递归搜索 现在 这里的问题是 当它满足我的上述要求时 如何突破递归 boolean bFi
  • Android Wear:在手持设备上启动服务

    我正在构建一个 Wear 应用程序 它将与手持设备上的 WearableListenerService 进行通信 但是 我想确保当应用程序在手表上启动时服务已启动并运行 我最初的想法是发送意图或广播消息来启动服务 但是 我一直无法弄清楚如何
  • 如何使用 zXing 在 Android 中创建 IntentIntegrator

    我想创建一个对象IntentIntegrator在安卓中 IntentIntegrator integrator new IntentIntegrator ZBarReaderActivity this Here ZBarReaderAct
  • 删除SD卡上的文件夹

    I tried File delete 但它不起作用 如何删除SD卡上的目录 我正在开发 Android 2 1 在删除目录本身之前 您必须将所有目录清空 请参阅here http www rgagnon com javadetails j
  • 在我授予许可后,表面视图不显示相机

    public View onCreateView LayoutInflater inflater Nullable ViewGroup container Bundle savedInstanceState View view inflat
  • 终端 (Mac) 上的 ndk-build 命令出错

    这是我在 bashrc 中的环境变量设置 export ANDROID SDK AndroidSDK android sdks export ANDROID NDK AndroidNDK android ndk r8d export PAT
  • 使用 Retrofit 2 添加标头以请求

    我正在尝试发送带有身份验证标头的请求 但服务器似乎无法识别客户端 我用了this https futurestud io tutorials android basic authentication with retrofit教程 并实现了
  • Android Facebook SDK 和 URL 方法成功形成好友对话框,但无法提交

    我开始认为这是一个错误 请证明我错了 我想以编程方式在 Facebook 上加好友 他们是唯一且肯定是该人在现实生活中认识的人 以下三种解决方案都具有相同的结果 成功的好友对话框 意味着个人资料名称 图片 指示操作的语句 与某人成为好友 以
  • Android键盘点击搜索输入时出现和消失

    我在用谷歌地图 Js API当我搜索一个地方时 我的输入搜索栏工作正常 当我通过 iPhone 设备使用它时 它也工作得很好 但是当我通过Android 设备然后键盘立即出现和消失 我已经找到了一些关于当我按下搜索栏时 android 键盘
  • GpsStatusListener:尽管状态为 GpsStatus.GPS_EVENT_FIRST_FIX,但修复中未使用卫星

    我向我的位置管理器添加了一个 GPS 状态侦听器 以便查看何时获得第一个修复 当我收到 GPS EVENT FIRST FIX 时 我会循环遍历所有卫星 但为什么修复中没有使用它们 usedInFix 我的日志对所有卫星都显示 错误 fin
  • 如果从超链接打开,应用程序将启动两次

    我正在开发一个应用程序 可以从多个地方启动 例如日历中的超链接 我在以下场景中面临问题 如果应用程序已启动并在后台运行 并且用户单击本机日历中的事件 超链接来启动应用程序 我的应用程序作为新实例启动两次 在正在运行的应用程序列表中 我可以看
  • 过滤列表视图并获取正确的 onclick 项目

    我有一个列表视图 并且已经实现了过滤 假设我有项目 A B 和 C 如果我在过滤框中输入 B 则只会显示项目 B 它是列表的位置 0 之前位于位置 1 因此 当我调用 onClick 项目时 我得到 id position 0 这导致显示有
  • Android HTTP PUT 请求

    谁能给我一个HTTP PUT请求 Android 的示例代码 假设您想使用 HttpURLConnection 要执行 HTTP PUT 请使用以下命令 URL url new URL http www example com resour
  • Android appwidget 远程视图未更新

    当我从某些活动更新小部件时 列表远程视图不会更新 我的意思是刷新自身 它会出现直到应用程序小部件的更新 日志显示 但不会进入列表视图的适配器以用新数据填充它 public void onUpdate Context context AppW
  • 尝试在后台使用 AsyncTask 解析 JSON 时强制关闭

    我是 Android 开发新手 正在研究 json 数据 我设法让解析工作 我想显示一个 ProgressDialog 我读到我需要使用 AsyncTask 但由于某种原因 一旦我将相同的工作代码放入 doInBackground 中 即使
  • startDrag 方法 已弃用且无法编译程序

    startDrag android content ClipData android view View DragShadowBuilder java lang Object int 已弃用 如何解决这个问题而又不失去对旧版本的兼容性 还有
  • 我收到“循环依赖”Android Dagger Hilt 错误

    我从头开始检查了所有内容 但找不到错误 我找不到错误 可能是什么 我收到以下编译错误 HomeViewModel java 6 error ComponentProcessor MiscError dagger internal codeg
  • 如何强制刷新 CallLog.Calls.CACHED_NAME 列?

    我的目标是从通话记录中收集所有未知的电话号码 这可以通过以下代码来实现 private static final String CALLOG PROJECTION CallLog Calls ID CallLog Calls CACHED
  • Visual Studio代码无法检测到模拟器设备或连接的电话

    I was running my app with vscode using Android emulator or my phone however all of a sudden vscode could not identify an

随机推荐

  • 作曲家安装不断消耗所有 RAM 内存

    在我正在开发的 Symfony 2 7 项目中 我通过 Composer 1 6 5 使用了很多包 几天后我无法再添加新包或重建composer lock 它总是内存不足 当我尝试添加新包时 我开始注意到这个问题 但问题不在于那个包 因为作
  • Java中使用什么方法来销毁你的对象[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 你能为我的问题举一个例子吗 抱歉 J
  • 是否有办法排除从父 POM 继承的工件?

    可以通过声明排除依赖项中的工件
  • Vim langmap 中断插件 (bépo)

    我正在使用 b po 键盘映射 http bepo fr http bepo fr 并寻求完美的 vim 映射 So far I used a long list of noremap http bepo fr wiki Vim B C3
  • 使用 python 为 Qt Designer 定制 Qt 小部件

    我正在尝试仅使用 Python 为 Qt Designer 编写自定义小部件 我正在遵循在网上找到的几个教程 但它们都不起作用 也没有任何接近我所说的最低工作示例的东西 所以我的问题是 使自定义小部件出现在 Qt Designer 的小部件
  • 离子侧菜单和带有嵌套视图的选项卡:选项卡嵌套视图的内容未显示

    我对两者都是新手AngularJS and Ionic 我正在创建一个应用程序 它具有sidemenu and tabs在菜单选项之一内 选项卡应使用以下方式呈现其内容nested view 所以没有简单的HTML here 我可以在侧菜单
  • 如何编写循环来运行数据框的 t 检验?

    我遇到了对数据框中存储的某些数据运行 t 检验的问题 我知道如何一一做 但效率很低 请问如何写一个循环来实现呢 例如 我在testData中获取了数据 testData lt dput testData structure list Lab
  • JavaScript 中的对象具有属性深度检查

    假设我们有这个 JavaScript 对象 var object innerObject deepObject value Here am I 我们如何检查是否value财产存在吗 我只能看到两种方法 第一 if object object
  • 水平滚动 UICollectionView 时捕捉到单元格的中心

    我知道有些人以前问过这个问题 但他们都是关于UITableViews or UIScrollViews我无法得到适合我的可接受的解决方案 我想要的是滚动浏览时的捕捉效果UICollectionView水平 很像 iOS AppStore 中
  • 不使用函数检测字符串结尾

    如何找出 PHP 字符串的结尾 我有一项任务 无需任何函数即可识别字符串长度 我知道 PHP 中的所有字符串都以空字节结尾 0 但我不知道最后一个符号之后的字符串元素 例如 这将不起作用 while a length 如何在不使用任何函数的
  • 在react-hook-form中react-table v7

    i am working on a complex react form where it has few controlled inputs along with grid table currently i am using react
  • 无法将调试器从 Visual Studio 2019 连接到本地 docker Linux 容器

    因此 我在本地计算机上使用 docker compose 在 docker 容器上运行了简单的 hello world net core 应用程序设置 问题是当我尝试使用 Debug gt Attach to Process gt Conn
  • 将源代码从 git 迁移到 TFVC

    我们使用的是Visual Studio Online 我们使用TFS的git支持 我们现在想停止使用 git 并使用 TFVC 将源代码迁移到 TFVC 的最佳解决方案是什么 假设您正在 VSO 中创建另一个项目并移动源代码 按照目前的情况
  • 我可以覆盖父元素的 z-index 继承吗?

    使用绝对位置时 有什么方法可以覆盖父元素的 z index 继承 我希望 2222 div 位于 0000 div 之上 div style background color green OOOO div div style backgro
  • 使用 C# 的服务 SACL ||使用 C# 获取具有 ACCESS_SYSTEM_SECURITY 权限的服务的句柄

    有人知道如何使用 C 获取远程服务上的 SACL 吗 我尝试了很多不同的方法 但基本上没有什么效果 我可以在本地计算机上获取 DACL 和 SACL 但在远程计算机上获取其中任何一个似乎都不可能 我所做的是创建一个名为ServiceSecu
  • 如何在 Linux 中检测通过 GUI 登录的用户

    我想在我的程序中捕获通过 GUI 登录的用户名 我的程序作为守护进程从 root 登录运行 如果非 root 用户通过 GUI 登录 我的程序应该会收到通知 我正在粘贴我当前的程序 该程序调用一个 perl 脚本 利用系统调用来检查当前登录
  • 如果尚未打开,请打开工作簿;如果已打开,则获取该引用

    我有一个场景 可以在另一个工作簿路径中的工作簿中进行一些更改 但问题是我需要检查工作簿是否已经打开 如果没有 我需要将该打开的实例获取到工作簿变量 这是我用来检查工作簿是否打开的代码 然后是打开的代码 Function IsFileOpen
  • 如何在Unity中集成xAPI / TinCan

    我是 xAPI TinCan API 的新手 所以我的问题可能有点笼统 但到目前为止我还没有找到任何可以帮助我的东西 我认为可以将 xAPI 集成到 Unity 项目 游戏或模拟 中 但我到底该怎么做呢 我可以阅读有关 xAPI 和 Uni
  • 显示用户密码和设备确认页面

    我试图在 Devise 邮件程序发送的确认页面中显示用户密码 确认页面为默认页面 Welcome email protected cdn cgi l email protection You can confirm your account
  • java.lang.IndexOutOfBoundsException:无效索引 7,大小为 7

    我正在尝试实现视图寻呼机 在我的视图寻呼机图像来自服务器 我能够显示和滚动 但当我到达最后一个图像时 它显示错误并且应用程序崩溃 以下是我的代码片段 public class Test Pager extends Activity priv