Intent.getSerializableExtra返回null

时间:2016-10-04 05:34:34

标签: android listview android-studio android-intent listadapter

我试图发送两个自定义对象,一个存储另一个的实例,另一个是另一个活动的意图。这两个都实现了Serializable,如下所示:

public class Story implements Serializable {

    private String title, author;
    private ArrayList<StorySegment> segments;

... }


class StorySegment implements Serializable {

    private String author;
    private String contents;

... }

在我的StoryOverviewAdapter课程中,我得到了这个:

public class StoryOverviewAdapter extends ArrayAdapter<Story> {

    public StoryOverviewAdapter(Context ctx, ArrayList<Story> stories) {
        super(ctx, 0, stories);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.story_overview_item, parent, false);
        }

        final Story story = getItem(position);

        TextView tvStoryTitle = (TextView) convertView.findViewById(R.id.tv_story_title);
        tvStoryTitle.setText(story.getTitle());
        TextView tvStoryOpening = (TextView) convertView.findViewById(R.id.tv_story_opening);
        tvStoryOpening.setText(story.getSegment(0).getContents());

        ImageView ivUserPicture = (ImageView) convertView.findViewById(R.id.iv_user_picture);
        ivUserPicture.setTag(position);
        ivUserPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show user profile
            }
        });

        LinearLayout llStoryLayout = (LinearLayout) convertView.findViewById(R.id.story_layout);
        llStoryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);
            }
        });

        return convertView;
    }
}

当我在接收活动中从我的意图中提取此信息时......

Intent intent = getIntent();
Story story = (Story) intent.getSerializableExtra("story");

...然后尝试测试我的代码,我明白了:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference

我不知道为什么。我想也许可以删除&#39; final&#39;从故事变量可以摆脱它,但我无法从内部的OnClickListener类中访问该变量。不知道该怎么做。

编辑:此外,这里的代码显示我添加了一个故事...

public class OverviewActivity extends AppCompatActivity {

    ArrayList<Story> stories = new ArrayList<Story>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);

        stories.add(new Story("Lorem Ipsum", "Firstname Lastname",
                new StorySegment("Firstname Lastname", "Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet...")));

        StoryOverviewAdapter storyOverviewAdapter = new StoryOverviewAdapter(this, stories);
        ListView listView = (ListView) findViewById(R.id.story_overview_list);
        listView.setAdapter(storyOverviewAdapter);
    }
}

4 个答案:

答案 0 :(得分:1)

我设法通过迁移尝试来获取意图的额外内容来解决这个问题......

    Intent intent = getIntent();
    story = (Story) intent.getSerializableExtra("story");

进入我的onCreate()方法,而不是在它之外。所以不要这样......

Intent intent = getIntent();
Story story = (Story) intent.getSerializableExtra("story");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

......我现在有了......

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    Intent intent = getIntent();
    Story story = (Story) intent.getSerializableExtra("story");
}

对我来说是一个愚蠢的疏忽......呃!

答案 1 :(得分:1)

您也可以使用GSON将自定义对象作为纯文本发送。无需序列化。

  String intentData = gson.toJson(storyObject);
  intent.putExtra("story", intentData);

在接收活动中,您可以写为

  Story object = gson.fromJson(intentData, Story.class);

答案 2 :(得分:0)

更改此

get "players/:player_id/posts" => "blogs#show", as: "player_posts"

#controller action:

class BlogsController < ApplicationController
  def show
    @player = Player.find(params[:player_id]) #players instance
    @blogs = @player.blogs
  end
end

进入这个

Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);

答案 3 :(得分:0)

使用此代码可以正常使用

private Context ctx;
public StoryOverviewAdapter(Context ctx, ArrayList<Story> stories) {
        super(ctx, 0, stories);
 this.ctx=ctx;
    }

Intent intent = new Intent(ctx, StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);