为特定的动态生成内容分配ID

时间:2014-07-31 15:56:44

标签: java android forms dynamic

我有一个模板表单,可以通过编程方式调用并放入视图中。在表单中,我有一个按钮,使用户可以在手机中搜索图像,然后保存图像。图像缩略图显示选择并保存的图像。缩略图放在一个ImageView中,其XML ID为" imgView"按钮旁边。为了保存生成的每个动态表单的图像,我在表单创建/检索时选择" imgView"生成然后为它分配一个从50001开始的整数id,如下所示:

case R.id.makeLayoutButton:
    v1 = vi.inflate(R.layout.form_template, null);

    //add view to the insertPoint
    ((LinearLayout) insertPoint).addView(v1);

    mStartActivityButton = (Button)v1.findViewById(R.id.start_file_picker_button1);
    mStartActivityButton.setOnClickListener(this);

    thumbnailContainer = (ImageView)v1.findViewById(R.id.imgView);
    thumbnailContainer.setId(setImageViewID);
    imageViewId = thumbnailContainer.getId();
    setImageViewID++;

    break;

然后,当用户选择图像时,从50000迭代到500020,然后在循环中搜索id为i的ImageView,如下所示:

ImageView imageView = (ImageView)findViewById(i);

发现后,我将图像放在适当的imageView中,如下所示:

if(i == imageView.getId()){
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    return;
}

问题是它在尝试检索和存储图像时崩溃并发出NULL POINTER EXCEPTION错误。它似乎没有找到分配了适当ID的ImageView。关于我哪里出错的任何线索?

开始图片浏览的代码:

case R.id.start_file_picker_button1:

// Create a new Intent for the file picker activity
Intent intent1 = new Intent(this, FilePickerActivity.class);
Intent i1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i1, RESULT_LOAD_IMAGE);
break;

代码查找图像并存储它:

@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();

        for(int i = 50000;i<=50020; i++){
            ImageView imageView = (ImageView)findViewById(i);
            if(i == imageView.getId()){
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                return;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决了它。如果有人感兴趣,这是代码,我意识到这可以进行优化和进一步修复。

int templateID = 1, inflatedID = 0, setImageViewID = 10, buttonID = 1, imageViewId;
Button b, target, bNext, testTemplate, mStartActivityButton, browseButton;
View v1, insertPoint;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;
ImageView thumbnailContainer;
private TextView mFilePathTextView;

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

public void Initialize(){
    //button for adding new forms
    b = (Button) findViewById(R.id.makeLayoutButton);
    b.setOnClickListener(this);

    bNext = (Button) findViewById(R.id.bNext);
    bNext.setOnClickListener(this);

    //get the template form to be duplicated, v1 is the form layout to be duplicated
    vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v1 = vi.inflate(R.layout.form_template, null);

    //container where forms will be contained in
    insertPoint = findViewById(R.id.questionsContainer);

    // Set the views
    mFilePathTextView = (TextView)findViewById(R.id.file_path_text_view);
    mStartActivityButton = (Button)findViewById(R.id.start_file_picker_button);
    mStartActivityButton.setOnClickListener(this);    
}

public void onClick(View v) {

    if(v.getId() == R.id.makeLayoutButton){

        v1 = vi.inflate(R.layout.form_template, null);

        ((LinearLayout) insertPoint).addView(v1);

        mStartActivityButton = (Button)v1.findViewById(R.id.start_file_picker_button);
        mStartActivityButton.setId(buttonID);

        mStartActivityButton.setOnClickListener(this);

        thumbnailContainer = (ImageView)v1.findViewById(R.id.imgView);
        thumbnailContainer.setId(setImageViewID);

        buttonID++;
        setImageViewID++;
    }

    for(int x=1;x<=10;x++){
            if(v.getId() == x){
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, v.getId());

        }
    }


    if(v.getId() == R.id.bNext){
        Intent b = new Intent(MakeQuestion.this, ChoosingTarget.class);
        startActivity(b);
    }

}

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

    if (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(requestCode + 9);

        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));   
    }
}