for 循环中的 OnClickListener
OnClickListener inside a for loop
我正在尝试使用 OnClick 方法中 For 循环中的 "i" 变量。"Error:(99, 95) error: local variable i is accessed from within inner class; needs to be declared final"。
我试图将这个 "i" 放在一个全局变量中,但后来我得到一个 "ArrayIndexOutOfBoundsException".
但是我想不出别的办法。
private void createTextViews(TextView[] textViewArray, LinearLayout linearLayout){
for (int i = 0; i < keys.length; i++) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 0, 0, 20);
TextView newView = new TextView(getActivity());
newView.setLayoutParams(layoutParams);
newView.setText("Watch the Trailer " + (i + 1));
newView.setTextSize(22);
newView.setTextColor(getResources().getColor(R.color.words));
newView.setBackgroundColor(getResources().getColor(R.color.button));
newView.setHighlightColor(getResources().getColor(R.color.background));
newView.setPadding(5, 5, 5, 5);
newView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(YOUTUBE_URL+keys[i]));
startActivity(intent);
}
});
linearLayout.addView(newView);
textViewArray[i] = newView;
}
}
我怎样才能做到这一点?
--------------
编辑
这是 ArrayIndexOutOfBoundsExeption 的日志:
11-03 01:08:13.866 32159-32159/app.com.example.android.popularmovies E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.com.example.android.popularmovies, PID: 32159
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at app.com.example.android.popularmovies.Detail_ActivityFragment.onClick(Detail_ActivityFragment.java:99)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18465)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
你得到越界异常的原因是因为在最后一个循环中你正在 i++ing 虽然它没有再次进入循环但是 class 变量的 i 现在已经出来了的约束。因此,将 i 作为 class 变量的整个方法不适用于您的情况。
实现此目的的一种方法是将 i 实际保存在视图中的标记中,然后从单击的视图中读取该标记。
newView.setTag(i)
然后在onClick中
v.getTag()
上面可以用来获取i
您必须初始化新的最终 int 值
添加final int ex = i;
在 setOnClickListener 之前
别忘了更改
Uri.parse(YOUTUBE_URL + keys[i]));
至
Uri.parse(YOUTUBE_URL + keys[ex]));
我正在尝试使用 OnClick 方法中 For 循环中的 "i" 变量。"Error:(99, 95) error: local variable i is accessed from within inner class; needs to be declared final"。
我试图将这个 "i" 放在一个全局变量中,但后来我得到一个 "ArrayIndexOutOfBoundsException".
但是我想不出别的办法。
private void createTextViews(TextView[] textViewArray, LinearLayout linearLayout){
for (int i = 0; i < keys.length; i++) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 0, 0, 20);
TextView newView = new TextView(getActivity());
newView.setLayoutParams(layoutParams);
newView.setText("Watch the Trailer " + (i + 1));
newView.setTextSize(22);
newView.setTextColor(getResources().getColor(R.color.words));
newView.setBackgroundColor(getResources().getColor(R.color.button));
newView.setHighlightColor(getResources().getColor(R.color.background));
newView.setPadding(5, 5, 5, 5);
newView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(YOUTUBE_URL+keys[i]));
startActivity(intent);
}
});
linearLayout.addView(newView);
textViewArray[i] = newView;
}
}
我怎样才能做到这一点?
--------------
编辑
这是 ArrayIndexOutOfBoundsExeption 的日志:
11-03 01:08:13.866 32159-32159/app.com.example.android.popularmovies E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.com.example.android.popularmovies, PID: 32159
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at app.com.example.android.popularmovies.Detail_ActivityFragment.onClick(Detail_ActivityFragment.java:99)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18465)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
你得到越界异常的原因是因为在最后一个循环中你正在 i++ing 虽然它没有再次进入循环但是 class 变量的 i 现在已经出来了的约束。因此,将 i 作为 class 变量的整个方法不适用于您的情况。
实现此目的的一种方法是将 i 实际保存在视图中的标记中,然后从单击的视图中读取该标记。
newView.setTag(i)
然后在onClick中
v.getTag()
上面可以用来获取i
您必须初始化新的最终 int 值
添加final int ex = i;
在 setOnClickListener 之前
别忘了更改
Uri.parse(YOUTUBE_URL + keys[i]));
至
Uri.parse(YOUTUBE_URL + keys[ex]));