3

I want to send the ImageView form one activity to another activity. When i am trying to pass the image view from Class A to Class B using bundle concept then in class B it's showing null pointer exception.Below is my code of class 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);         

    }

}

here the error in class B:-

Murali
  • 109
  • 2
  • 3
  • 7
  • How are you passing the ImageView? – Brian Oct 18 '11 at 20:35
  • Can you post a concise, complete code example of what you are trying to do. You can remove all the extra code that isn't specific to the problem you are having. It makes it much easier for us to help you. – Justin Breitfeller Oct 18 '11 at 21:08

2 Answers2

9

You shouldn't be passing views between activities. You should use extras to pass the location of the image in the intent and load it again in your new activity.

Ex:

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

And in you receiving activity:

    String path = getIntent().getStringExtra("imagePath");
    Drawable image = Drawable.createFromPath(path);
    myImageView.setImageDrawable(image);
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
5

You absolutely do not want to pass an image view from one activity to another. Views keep a reference to their ViewContainer and parent Activity. This prevents the Garbage Collector from clearing up memory when the Activity that owns the View gets hidden or closes. Instead you should pass the information required to replicate the ImageView from activity to another using Intents.

slayton
  • 20,123
  • 10
  • 60
  • 89