【发布时间】: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