在Android图像库上选择NullPointer异常

时间:2013-03-01 07:15:43

标签: java android

我正在使用android图库,我想通过点击图库项目选择图片标记ID,并将该ID放入HashMap(param_ids)。我使用了以下代码。但在我点击图库项目之前,这在我的行上给了NullPointerException

  param_ids.put("facial_appearance_id",view.getTag().toString());

这将在android 2.3上正常运行,但这会在NullPointerException上提供android 4.0.3。那我怎么能解决这个问题呢?

Gallery faceGallery= (Gallery) findViewById(R.id.gallery);
faceGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
    param_ids.put("facial_appearance_id",view.getTag().toString());
            }
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

        }
 });

1 个答案:

答案 0 :(得分:0)

使用Gallery OnItemClickListener()

 faceGallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,
                long id) {

            //Do your stuff

        }
    });