详细,
我有 A类,并且在那个班级我有
public static ArrayList<String> video_title = new ArrayList<String>();
和 video_title的所有值都在A类中填充
我要从A类来到 B类并以A.video_title.get(索引)的身份访问ArrayList ..好吧这样可以正常工作..
现在出现了真正的问题,
我正在从B类链接打开Android默认Web视图,当我回到我的Activity时(显然当我从Web视图按回来打开我的上一个活动类B)时,它会给IndexOutOfBoundsException
并说Invalid index 2, size is 0.
1}}
更奇怪的是,它在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时,所有活动的所有变量都被清除了。为什么会发生这种情况?甚至我将变量声明为静态。出了什么问题?
答案 0 :(得分:0)
当你通过移除你的arraylist Pause
你的活动android kitkat 4.4.4释放内存。您可以声明静态arraylist以避免此问题。或者在onPause
保存偏好设置中的arraylist并加载回onResume
。