如何在android中的活动之间传递图像视图

时间:2011-10-18 20:30:55

标签: android

我想将ImageView表单的一个活动发送到另一个活动。当我试图使用bundle概念将图像视图从A类传递给B类时,然后在B类中显示空指针异常.Below是我的A类代码。

  public class First extends Activity {
    Cursor cursor1;
    boolean y,n;
    ImageView login;
    Button  enroll;
    String AndroidId;
    SQLiteDatabase db1,db;
    private ContentValues values;
    DopenHelper helper;


    String TableName = "tbl_finger";
    ImageView FingerImageData;
    String fingerID,DivID,enrolledtype;



    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();        
        helper.close();
        db.close();
        db1.close();

    }

    protected void onResume() 
    {
        // TODO Auto-generated method stub
        super.onResume();
        helper =new DopenHelper(getApplicationContext());
        db=helper.getWritableDatabase();
        db1=helper.getWritableDatabase();
        values=new ContentValues();
        enroll=(Button)findViewById(R.id.enroll_button);



        FingerImageData = (ImageView)findViewById(R.id.fingerid);
         AndroidId = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);
         //"[B@40520c28";
         //"com.a1technology.remoteid.First@4050f508";      

            helper = new DopenHelper(First.this);           

            cursor1 = db1.rawQuery("SELECT  template,enrolled,deviceID FROM " + TableName,  null);

            try {

                db1 = this.openOrCreateDatabase("remoteid.db", MODE_PRIVATE, null);

                if(cursor1 != null )
                {
                    if(cursor1.moveToFirst())
                    {
                        do {
                            DivID = cursor1.getString(cursor1.getColumnIndex("deviceID"));
                            fingerID = cursor1.getString(cursor1.getColumnIndex("template"));
                            enrolledtype = cursor1.getString(cursor1.getColumnIndex("enrolled"));

                            //Toast.makeText(getApplicationContext(), DivID, Toast.LENGTH_LONG).show();

                        }while (cursor1.moveToNext());
                    }
                }

            }

            catch(Exception e) {
                Log.e("Error", "Error", e);

            } finally {
                if (db1 != null)
                    db1.close();

            }   

            cursor1.close();  

             final Bundle bundle = new Bundle();

        enroll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {


                if((FingerImageData.getContext().toString()).equals(fingerID))
                {
                    Intent menuintent=new Intent(First.this,Menu.class);
                    bundle.putString("ThumbInfo2", FingerImageData.toString());                 
                    menuintent.putExtras(bundle);
                    startActivity(menuintent);

                }  
                /*else if((FingerImageData.getContext().toString()).equals(fingerID) && AndroidId.equals(DivID)  )
                    {
                        Intent menuintent=new Intent(First.this,Menu.class);
                        startActivity(menuintent);

                    } */
                else 
                {

                    values.put("template", FingerImageData.getContext().toString());
                    values.put("enrolled", getEnrolledType());
                    values.put("DeviceID", AndroidId.getBytes().toString() );
                    db.insert("tbl_finger", "Id", values);


                    bundle.putString("ThumbInfo1", FingerImageData.toString());                     
                    Intent enroll=new Intent(First.this,Enroll.class);
                    enroll.putExtras(bundle);
                    startActivity(enroll);                      

                }

            }

            private String getEnrolledType() {
                // TODO Auto-generated method stub
                //AndroidId.equals(DivID) && 
                if((FingerImageData.getContext().toString()).equals(fingerID)){
                    return "N";

                }
                else {
                    return "Y";
                }

            }
        });  

    }


    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.first);         

    }

}

这里是B组的错误: -

2 个答案:

答案 0 :(得分:9)

您不应该在活动之间传递视图。您应该使用额外内容来传递意图中图像的位置,并在新活动中再次加载它。

例如:

    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra("imagePath", pathToImage);
    startActivity(intent);

在你接受活动时:

    String path = getIntent().getStringExtra("imagePath");
    Drawable image = Drawable.createFromPath(path);
    myImageView.setImageDrawable(image);

答案 1 :(得分:5)

您绝对想要将图片视图从一个活动传递到另一个活动。 Views保留对ViewContainer和父Activity的引用。这可以防止垃圾收集器在拥有Activity的{​​{1}}被隐藏或关闭时清除内存。相反,您应该使用View传递将ImageView从活动复制到另一个活动所需的信息。