gpt4 book ai didi

Android 动态 ImageView

转载 作者:行者123 更新时间:2023-11-30 03:59:29 26 4
gpt4 key购买 nike

我正在制作一个自定义布局,我想在其中动态添加图库中的 ImageView 。但是,一旦我从图库中挑选图像,我就被逼近了。我无法得到相同背后的错误是什么。

这是我的代码..

public class Dynamic extends Activity {
int i;
ImageView[] img_items;
ArrayList<String> values = new ArrayList<String>();
LinearLayout imageLayout;
Button btn;
private static int RESULT_LOAD_IMAGE = 1;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_load);

imageLayout = (LinearLayout)findViewById(R.id.image_layout);
btn = (Button)findViewById(R.id.btn_ADD);
img_items = new ImageView[values.size()];

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

values.add("AA");

Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

}
});


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

/*ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));*/


//Creating the image layouts dynamically
for( i=0;i<values.size();i++){

img_items[i] = new ImageView(Dynamic.this);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

params.setMargins(10, 10, 0, 0);
img_items[i].setLayoutParams(params);

}

img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageLayout.addView(img_items[i]);


}
}
}

日志猫

10-05 16:52:03.422: D/AndroidRuntime(451): Shutting down VM
10-05 16:52:03.422: W/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-05 16:52:03.549: E/AndroidRuntime(451): FATAL EXCEPTION: main
10-05 16:52:03.549: E/AndroidRuntime(451): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {com.example.dynamicloading/com.example.dynamicloading.Dynamic}: java.lang.ArrayIndexOutOfBoundsException
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-05 16:52:03.549: E/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 16:52:03.549: E/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:521)
10-05 16:52:03.549: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-05 16:52:03.549: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-05 16:52:03.549: E/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
10-05 16:52:03.549: E/AndroidRuntime(451): Caused by: java.lang.ArrayIndexOutOfBoundsException
10-05 16:52:03.549: E/AndroidRuntime(451): at com.example.dynamicloading.Dynamic.onActivityResult(Dynamic.java:81)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
10-05 16:52:03.549: E/AndroidRuntime(451): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
10-05 16:52:03.549: E/AndroidRuntime(451): ... 11 more
10-05 16:57:04.140: I/Process(451): Sending signal. PID: 451 SIG: 9

最佳答案

您需要将 i 值减一,因为 i 在 for 循环中递增,例如您迭代了 5 次,所以for 循环将迭代 5 次,从 0 开始,到 4 结束,但此时 i value 首先递增然后它会检查 2 表达式的长度和 for i 当时 i value 和 values.size() 是否是同样,它终止 for 循环执行其余代码

已编辑

if(i==values.size() && i>0)
i--;

img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageLayout.addView(img_items[i]);

还有一件事你需要每次在 onActivityResult() 中设置 i=0 否则你总是得到 i 最后一个值

关于Android 动态 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12745294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com