【问题标题】:Very strange exception ,IndexOutOfBoundsException in 4.4 but working well in 5.0 : Android非常奇怪的异常,4.4 中的 IndexOutOfBoundsException 但在 5.0 中运行良好:Android
【发布时间】:2015-06-19 06:02:04
【问题描述】:

详细,

我有 A 类,并且在那个类中我有

  public static ArrayList<String> video_title = new ArrayList<String>();

并且video_title的所有值都填写在A类中

比我要去 class B 从 class A 并以 A.video_title.get(index) 的形式访问 ArrayList.. 好的,除了例外..

现在真正的问题出现了, 我正在从 B 类的链接打开 Android 默认 Web 视图,当我回到我的 Activity 时(显然,当我从 Web 视图按返回时,它会打开我的最后一个活动类 B),它给出了 IndexOutOfBoundsException 并说 Invalid index 2, size is 0.

更奇怪的是,它在Android 4.4.4 中出现错误,但在

中工作正常
Android 5.0.2

我的两个类的代码如下(我只分享必要的代码,因为我的应用程序有一些敏感代码),

A类:

public class A extends Activity{

public static ArrayList<String> video_title = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.list_of_video);


    new AsyncTask<Void, Void, Void>()
    {


        protected void onPreExecute()
        {
            video_title.clear();

        };

        @Override
        protected Void doInBackground(Void... params)
        {
            // TODO Auto-generated method stub

            try
            {
                JSONObject obj = Handle_json.HTTPGet(“here is my url from data is come“);

                nextPageToken = obj.getString("nextPageToken");

                JSONArray items = obj.getJSONArray("items");

                for (int i = 0; i < items.length(); i++)
                {

                    JSONObject jobj = items.getJSONObject(i);

                    video_title.add("" + omg.getString("title"));

                }

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

            return null;
        }

        protected void onPostExecute(Void result)
        {
            pDialog.dismiss();

        };

    }.execute();

}



@Override
public void onBackPressed()
{
    // TODO Auto-generated method stub
    super.onBackPressed();
    overridePendingTransition(R.drawable.in2, R.drawable.out2);
  }
 }

B类:

public class B extends YouTubeFailureRecoveryActivity implements OnFullscreenListener{

TextView video_detail_title, video_detail_title2, video_detail_views,
    like_count, dislike_count, video_detail_desc;
ImageView video_detail_iv;

ProgressBar video_detail_pb;

int position = 0;

RelativeLayout main_content;

private View videoBox;
private View closeButton;
YouTubePlayerView youTubeView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.video_detail);

    final Bundle bundle = getIntent().getExtras();
    position = bundle.getInt("position");

    main_content = (RelativeLayout) findViewById(R.id.main_content);

    videoBox = findViewById(R.id.video_box);
    closeButton = findViewById(R.id.close_button);
    videoBox.setVisibility(View.INVISIBLE);

    closeButton.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            onClickClose();
        }
    });

    findViewById(R.id.video_detail_iv).setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            if (videoBox.getVisibility() != View.VISIBLE)
            {
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                {
                    // Initially translate off the screen so that it can be animated in from below.
                    videoBox.setTranslationY(videoBox.getHeight());
                }
                videoBox.setVisibility(View.VISIBLE);
            }

            // If the fragment is off the screen, we animate it in.
            if (videoBox.getTranslationY() > 0)
            {
                videoBox.animate().translationY(0).setDuration(300);
            }

        }
    });


    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);

    video_detail_title = (TextView) findViewById(R.id.video_detail_title);
    video_detail_title.setText("" + A.video_title.get(position));


}

@Override
public void onBackPressed()
{
    // TODO Auto-generated method stub
    super.onBackPressed();
    overridePendingTransition(R.drawable.in2, R.drawable.out2);
}

@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1, boolean arg2)
{
    // TODO Auto-generated method stub
    if (!arg2)
    {
        arg1.cueVideo(Show_List_Of_Video.video_id.get(position));
    }
}

@Override
protected Provider getYouTubePlayerProvider()
{
    // TODO Auto-generated method stub
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

private boolean isFullscreen;

@Override
public void onFullscreen(boolean arg0)
{
    // TODO Auto-generated method stub
    this.isFullscreen = arg0;

    closeButton.setVisibility(isFullscreen ? View.GONE : View.VISIBLE);
    if (isFullscreen)
    {
        videoBox.setTranslationY(0);

    }

}

public void onClickClose()
{
    ViewPropertyAnimator animator = videoBox.animate().translationYBy(videoBox.getHeight()).setDuration(300);
    runOnAnimationEnd(animator, new Runnable()
    {
        @Override
        public void run()
        {
            videoBox.setVisibility(View.INVISIBLE);
        }
    });
}

@TargetApi(16)
private void runOnAnimationEnd(ViewPropertyAnimator animator, final Runnable runnable)
{
    if (Build.VERSION.SDK_INT >= 16)
    {
        animator.withEndAction(runnable);
    }
    else
    {
        animator.setListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                runnable.run();
            }
        });
    }
  }

 }

添加后:

我发现当我回到活动 B 时,所有活动的所有变量都被清除了。为什么会这样?即使我将变量声明为静态的。怎么了?

【问题讨论】:

  • 尝试使用最新的 JDK 1.7 或 1.8 进行编译
  • 如果这是 JDK 的问题,那么它也应该在 5.0.2 中给出错误

标签: android android-5.0-lollipop indexoutofboundsexception android-4.4-kitkat onresume


【解决方案1】:

当您Pause您的活动时,android kitkat 4.4.4 通过删除您的数组列表来释放内存。你可以声明一个静态数组列表来避免这个问题。或在onPause 上将arraylist 保存在首选项中并重新加载到onResume

【讨论】:

  • 您是否尝试在onCreate 内创建列表? video_title = new ArrayList&lt;String&gt;();
  • 我刚刚在您发表评论后尝试过,但它不起作用!
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2021-07-14
相关资源
最近更新 更多