in detail,
i have class A and in that class i have
public static ArrayList<String> video_title = new ArrayList<String>();
and all the values of video_title are filled in class A
than i am going to class B from class A and accessing ArrayList as A.video_title.get(index).. ok thats working fine as excepted..
Now real problem occurs,
I am opening Android default web view from link of class B and when i come back to my Activity (obviously when i press back from web view it opens my last activity class B), it gives IndexOutOfBoundsException
and says that Invalid index 2, size is 0.
More strange is that, it gives error in Android 4.4.4
but working fine in
Android 5.0.2
my code of both class as below (i am sharing only necessary code because my app have some sensitive code ),
Class 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);
}
}
Class 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();
}
});
}
}
}
Added After :
i found that all variable of all activity is cleared when i back to the activity B. why this is happening? even i declare variable as a static. what is going wrong?
When you
Pause
your activity android kitkat 4.4.4 free memory by removing your arraylist. You can declare a static arraylist to avoid this problem. or ononPause
save arraylist in preferences and load back ononResume
.