如何动态添加Image到动态创建的ImageView

How to dynamically add Image to dynamically created ImageView

我正在尝试将图像动态添加到动态创建的 ImageView。 我 select 图像后应用程序崩溃。我用非动态创建的视图进行了尝试,效果很好。我不确定我的问题出在哪里。

我已经膨胀了包含 ImageView 的布局,我是否应该也膨胀 ImageView?我不确定我的问题出在哪里。这是我的主要class.

public class MainActivity extends AppCompatActivity {

int clickCounterIndex = 0;
LinearLayout picsLayout;
LayoutInflater inflater;
View picItem;

Intent intentForPic;
int RESULT_LOAD_IMAGE = 1;
ImageView pic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
    pic = (ImageView) findViewById(R.id.picImageView);

    picsLayout = (LinearLayout)findViewById(R.id.picsLayout);
    inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public void addStuff(View view) {

    intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
    intentForPic.setType("image/*");
    startActivityForResult(intentForPic, RESULT_LOAD_IMAGE);

    picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);
    picsLayout.addView(picItem, clickCounterIndex);
    clickCounterIndex++;

}

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

    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        Uri imageUri = data.getData();
        pic.setImageURI(imageUri);

    }
}
}

主布局文件。

<LinearLayout android:id="@+id/picsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
    android:text="click me!"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="addStuff"/>

</LinearLayout>

动态添加的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
tools:context="com.example.k0k0.thenextsnapchat.imageuploaddemo.MainActivity">

<ImageView
    android:id="@+id/picImageView"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:src="@android:drawable/ic_popup_disk_full"/>

<ProgressBar
    android:id="@+id/picUploadProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:progress="100"/>

<ImageButton
    android:id="@+id/xImageButton"
    android:src="@android:drawable/btn_minus"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="38dp"
    android:layout_marginBottom="38dp"/>

</LinearLayout>

因为您的 ImageView 是您动态创建的视图的一部分。所以你必须像这样引用它:

picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);

pic = (ImageView) picItem.findViewById(R.id.picImageView);

picsLayout.addView(picItem, clickCounterIndex);
clickCounterIndex++;