【问题标题】:Assigning ID to specific dynamically generated content将 ID 分配给特定的动态生成的内容
【发布时间】:2014-09-23 16:15:36
【问题描述】:

我有一个以编程方式调用并显示在视图中的模板表单。在表单中,我有一个按钮,使用户能够在手机中搜索图像,然后保存图像。图像缩略图显示选择和保存的图像。缩略图放置在 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;
            }
        }
    }
}

【问题讨论】:

    标签: java android forms dynamic


    【解决方案1】:

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

    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));   
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2013-02-28
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      相关资源
      最近更新 更多