2014-06-27 18 views
1

我想通過意圖傳遞一個接口對象。爲什麼我正在獲取Parcelable遇到IOException時試圖通過意圖傳遞接口對象

public interface ILockKeyConfig extends Serializable{ 
    public void onKeyConfigChage(); 
} 

// then I am doing in Activity A 
Intent intent = new Intent(LockScreenSettings.this,ButtonCustomization.class); 
      ILockKeyConfig iLockKeyConfig = new ILockKeyConfig() { 

       @Override 
       public void onKeyConfigChage() { 
        System.out.println("onKeyConfigChage called"); 

       } 
      }; 
      Object[] keConfig = {lockScreenKeyList.get(position),iLockKeyConfig}; 
      intent.putExtra("KeyConfigObj", keConfig); 
      startActivity(intent); 


//and in Activity B I am doing... 
private void getIntentData() { 
    Object[] KeyConfigObj = (Object[]) getIntent().getSerializableExtra("KeyConfigObj"); 
    keyConfig = (KeyConfig) KeyConfigObj[0]; 
    iLockKeyConfig = (ILockKeyConfig) KeyConfigObj[1]; 
} 

,但上面的代碼我收到以下錯誤:

`06-27 15:18:04.648: E/AndroidRuntime(5483): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object

這是怎麼回事錯在這裏?

+0

你穿越的對象'Intent'必須是'Serializable'或'Parcelable'。 –

+0

但我擴展Serializable在我的界面? – user3782810

+0

哦,我明白了。但是你的實際類是非靜態的內部類,所以它不能在任何其他地方被反序列化,而是在創建它的類實例中。 –

回答

0

您可以實現parcelable或將您的序列化對象放入ArrayList中。 ArrayList實現Serializable,只要你的ArrayList有可序列化的對象就可以接受。

putExtra(String name, Serializable value) 

public Intent putExtra (String name, Parcelable[] value) 
相關問題