簡體   English   中英

Intent類型的方法putExtra(String,boolean)不適用於參數(String,CarouselDataItem)

[英]The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem)

我在日食說明中出現錯誤

"The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, CarouselDataItem)"

如何避免這種情況? 我正在嘗試在Android中實現圖片輪播,但似乎無法避免此問題。 我嘗試將docu的類型更改為boolean-但是,這只會引起更多問題。

任何建議,不勝感激。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         CarouselDataItem docu =  (CarouselDataItem) m_carouselAdapter.getItem((int) arg3);

            Intent intent = null;

            if (docu .equals("John F. Kennedy"))
                intent = new Intent (MainActivity.this, Audio.class);
            if (docu .equals("Lyndon B. Johnson"))
                intent = new Intent (MainActivity.this, Video.class);
            if (docu .equals("Richard Nixon"))
                intent = new Intent (MainActivity.this, Photos.class);
            if (docu .equals("Gerald Ford"))
                intent = new Intent (MainActivity.this, Written.class); 
            if (intent != null) {
                intent.putExtra("KEY", docu );
                startActivity(intent);
            }
        };

    public void onNothingSelected(AdapterView<?> arg0) {}

}

任何建議,不勝感激。

您正在嘗試通過活動傳遞自定義對象。 在這種情況下,要在“活動”之間傳遞的任何類型的對象都必須實現ParcelableSerializable接口。

可序列化更易於實現,但官方建議使用可打包。 您可以在此線程中找到更多信息。

您不能使用docu調用putExtra,因為docu的類型為CarouselDataItem,並且Intent中不存在名為putExtra且參數為String和CarouseldataItem的方法。 相反,讓CarouselDataItem擴展Parcelable,使它能夠在活動之間發送。

有關良好的示例,請參見有關可包裹的博客文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM