【问题标题】:Passing object Bundle intent in Serializable class在可序列化类中传递对象捆绑意图
【发布时间】:2013-11-24 14:04:09
【问题描述】:

我必须提到我是 Android 和 Java 的新手。我正在尝试一个星期来解决这个问题。 我有 Serializable 类,其中填充了 http json 的对象,我正在使用适配器填充列表视图,一切正常,但是当我想将一个对象传递给另一个类时,它强制关闭,请如果有人可以更正我的代码。

这个 void 在 Serializable 类中

  public void save(){
 Intent intent =  new Intent();
 Bundle extra = intent.getExtras();
  intent.putExtra("title", getTitle()); 

           }

当我尝试这个时:

Intent intent =  new Intent(this, Fragment2.class);  i got error The constructor   
Intent(FeedItem, Class<Fragment2>) is undefined

这是我想使用传递对象的片段类

     Intent intent = getActivity().getIntent();
 Bundle extras = intent.getExtras();
 FeedItem feedItem = (FeedItem)getActivity().getIntent().getSerializableExtra("title");
 String title = feedItem.getTitle();
 Toast.makeText(getActivity(), title, Toast.LENGTH_LONG).show(); 

error 局部变量 extras 的值没有被使用

非常感谢。

【问题讨论】:

  • FeedItemFragment2 是否都在扩展 Activity
  • getTitle() 是返回 String 还是 FeedItem 对象?
  • Fragment2 扩展了 Fragment 和 FeedItem 实现了 Serializable
  • Title 是一个对象,我用它 getTitle 把它放在 Intent 中

标签: java android serialization android-intent bundle


【解决方案1】:

片段是这样添加的

FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            ft.replace(resourceidwhereyouwantadd(R.id.view), new YourFragment());
            ft.commit();

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 2015-02-03
    • 2017-10-22
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 2013-12-28
    • 2021-03-08
    • 1970-01-01
    相关资源
    最近更新 更多