2011-10-18 33 views
3

我想发送一个活动ImageView窗体到另一个活动。当我试图使用捆绑的概念,然后在B类它显示空指针传递从A类图像视图,以B级exception.Below是我的A类代码我怎样才能通过在Android的活动之间的图像视图

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); 
     //"[[email protected]"; 
     //"[email protected]";  

      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类错误: -

+0

你是如何传递ImageView的? – Brian

+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); 
5

你绝对想从一个活动传递的形象图到另一个。 Views保留对其ViewContainer和父母Activity的引用。这可以防止Garbage Collector在拥有ViewActivity隐藏或关闭时清除内存。相反,您应该通过使用Intents从活动复制ImageView所需的信息。