2017-07-09 4 views
0

すべての同様の質問が返されましたが、結果は返されません。 intentServiceからparcelableを実装する1つのojbectをアクティビティに渡すことはできません。同じコードは、アクティビティから別のアクティビティにデータを渡すときにうまく機能します。 ParcelableArrayListExtraを渡すことはできますが、Parcelableオブジェクトは渡すことはできません。それは私のプロジェクトでオブジェクトを渡すための必要条件です。私がいることをsuposseIntellServiceからPutExtraとしてParcelableオブジェクトを別のアクティビティに渡すことはできません。

List<User> userList= getIntent().getParcelableArrayListExtra(EXTRA_INSECTS); 
Insect selected = getIntent().getExtras().getParcelable("selectedUser"); 
+0

**何を意味し、どのような "私は1つのアクティビティにintentServiceからparcelable実装ojbectを渡すことはできません" 詳細に説明してください** "パーセルブルオブジェクトではない"という意味です。あなたの症状は何ですか? – CommonsWare

+0

例外はありますか? – Alex

+0

どのAPIレベルでテストデバイスを使用しますか? – mac229

答えて

0

IntentService:

Intent action = new Intent(this, QuizActivity.class); 
    intent.putParcelableArrayListExtra("userList",name); 
    Random position = new Random(); 
    int randomPosition =position.nextInt(3); 
    intent.putExtra("selectedUser",name.get(randomPosition)); 
    action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

SecondActivity コードは(いけない右全体のコードを公開する必要があり、全体のコードは、これらの行を除いて取り組んでいる) CODE以下に示します。問題は、サービスからアクティビティへのAPI 25+にカスタムParcelableを置くことである可能性があります。

このanswerは、Parcelableを自分自身をbyte []に​​変換するという形で回避策があります。

ArrayListではなく、単一のParcelableオブジェクトを渡すことから始めます。

public static void putExtra(Intent intent, String key, Parcelable parcelable) { 
    byte[] bytes = marshall(parcelable); 
    intent.putExtra(key, bytes); 
} 

public static <T> T getExtra(Intent intent, String key, Parcelable.Creator<T> creator) { 
    byte[] bytes = intent.getByteArrayExtra(key); 
    return ParcelableUtils.unmarshall(bytes, creator); 
} 
+0

許可されていません... –

0

以下試してみて、あなたはgetParcelableArrayListExtraが同じname.youを使用し得るときParcelableとUserクラスを実装する必要があります。

Intent action = new Intent(this, QuizActivity.class); 
intent.putParcelableArrayListExtra("userList",name); 
Random position = new Random(); 
int randomPosition =position.nextInt(3); 
intent.putExtra("selectedUser",name.get(randomPosition)); 
action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 
SecondActivityで

ArrayList<User> userList = getIntent().getParcelableArrayListExtra("userList"); 
for(User user : post){ 
    Log.d("user", user.getString()); 
} 
関連する問題