0

我有这个正在开发的演示应用程序。它包含两个活动,A 和 B。一个活动有一个字符串数组列表和一个菜单按钮。B 活动有一个数组列表,但它只显示在 A 中选择的项目。它工作正常,除了我无法从任何其他活动(例如:活动 C)直接访问 B。它给了我空指针异常。也就是说,只有当我按下菜单按钮并单击显示活动 B 选项时,我才能看到活动 B 的内容。代码如下

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.action_settings:
        Intent myIntent = new Intent(this, MainActivity.class);
        myIntent.putExtra("key", finalList);
        startActivity(myIntent);

        break;
    }

    return false;
}

是因为我在这里放了Extra,所以我可以通过活动A的菜单选项访问活动B(就像只有从菜单中选择活动B时才会显示活动B)?我怎样才能做到这一点,以便我可以从任何我想要的地方访问活动 B。

4

0 回答 0