按下后退按钮时的意图错误

时间:2015-10-28 13:39:56

标签: android android-intent android-studio

我有3项活动,A - > B - > C 活动A具有列表视图,活动B具有子列表视图,C具有活动B列表视图项目的详细信息

分类活动 - >故事活动 - > StoryDe​​tailActivity

一切都很好,直到我点击应用程序崩溃的StoryDe​​tailActivity上的后退按钮,这里是logcat

  

10-28 09:35:48.125 5607-5607 / com.example.zack.stories E / AndroidRuntime:at com.example.zack.stories.activities.StoriesActivity.onCreate(StoriesActivity.java:42)

这是第42行

Categories category=getIntent().getExtras().getParcelable(EXTRA_CAT);

这是storyActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_newslist);
    final ListView mlist=(ListView) findViewById(R.id.lsv_cat_item);
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     Categories category=getIntent().getExtras().getParcelable(EXTRA_CAT);


    mAdapter = new StoryListAdapter(getApplicationContext(),0);

    RestAdapter restAdapter=new RestAdapter.Builder().setEndpoint(getString(R.string.categories_feed)).build();
    final CategoriesApiInterface categoriesApiInterface=restAdapter.create(CategoriesApiInterface.class);
    categoriesApiInterface.getStreams2(category.getCat_id(), new Callback<List<Stories>>() {
        @Override
        public void success(List<Stories> stories, Response response) {
            if (stories == null || stories.isEmpty()) {
                return;}

            for(Stories stories1 : stories){
                mAdapter.add(stories1);

            }
            mAdapter.notifyDataSetChanged();
            mlist.setAdapter(mAdapter);

        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("Stories", "Retrofit error " + error.getMessage());
        }
    });


mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent( getApplicationContext(), StoryDetailActivity.class );
        intent.putExtra(StoryDetailActivity.EXTRA_STORY, mAdapter.getItem(position));
        startActivity(intent);
    }
});


}

和StorydetailActicity

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

    Toolbar toolbar = (Toolbar) findViewById( R.id.tool_bar );
    setSupportActionBar( toolbar );
    getSupportActionBar().setDisplayHomeAsUpEnabled( true );

    Stories story=getIntent().getExtras().getParcelable(EXTRA_STORY);

    TextView story_title = (TextView) findViewById( R.id.story_title );
    TextView story_body = (TextView) findViewById( R.id.story_body );

    story_title.setText( story.getStory_title() );
    story_body.setText(story.getStory_body());

}

1 个答案:

答案 0 :(得分:0)

从您提供的详细信息和堆栈跟踪看起来,当您启动StoriesActivity时,您的StoryDetailsActivity会被销毁。操作系统的内存可能很低。

因此,只要您按Back Button上的StoryDetailsActivity,就会再次创建StoriesActivity,并且该活动意图没有 {{1}的价值}。

如果您想要在同一屏幕上导航,当我为EXTRA_CAT调用startActivity时,我建议在startActivityForResult()中使用StoriesActivity

如果他们负责重新创建活动(主要不是),还要查看清单文件中活动标签中的启动模式。