使用向另一个 Activity 发送意图有什么问题?

What is wrong with using sending intent to another Activity?

我要发送带有 ArrayList 的捆绑包,但我的代码导致了 NullPointerException。但是不知道为什么。

主要Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);
    context = getApplicationContext();
    txtName = findViewById(R.id.name);
    txtDepartment = findViewById(R.id.department);
    txtUndergred = findViewById(R.id.undergred);
    txtUrl = findViewById(R.id.url);
    txtPhone = findViewById(R.id.phone);
    btnphone = findViewById(R.id.btnphone);
    btnlogin = findViewById(R.id.btnlogin);
    btnweb = findViewById(R.id.btnweb);

    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = txtName.getText().toString();
            String department = txtDepartment.getText().toString();
            String undergrad = txtUndergred.getText().toString();
            Bundle Sender = new Bundle();
            ArrayList list = new ArrayList<>();
            list.add(name);
            list.add(department);
            list.add(undergred);
            Sender.putStringArrayList("list", list);
            ArrayList shit = Sender.getStringArrayList("list");
            Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
            intent.putExtra("Sender", Sender);
            startActivity(intent);
        }
    });
}

MainActivity2(此处接收)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        txtUrl = findViewById(R.id.url);
        txtPhoneNum = findViewById(R.id.phoneNum);
        button0 = findViewById(R.id.goback);
        Intent passedIntent = getIntent();
        if (passedIntent != null) {
            Bundle mybundle = passedIntent.getBundleExtra("Sender");
            ArrayList list = mybundle.getStringArrayList("Sender");
            Toast.makeText(getApplicationContext(), "Student info: "
                    +list.get(0)+list.get(1)+list.get(2),duration).show();
        }
        String Url = txtUrl.getText().toString();
        String PhoneNum = txtPhoneNum.getText().toString();
}

这里,哪一部分是错误的? Arraylist 被添加到包中,但每次抛出 NullPointerException

问题


您正在传递一个键,然后尝试使用另一个键获取值。这是错误的。为了让大家明白,我举个例子→

案例:您的店里有很多tee-shirts。您订购了一些 tee-shirts 进行销售,但只得到了 redgreen tee-shirts。现在,一个风俗来了,他问yellowtee-shirts,那你就说没有。这在编码术语中称为 null 。你这边也是一样的情况。让我们检查解决方案。

解决方案


当您使用密钥发送值时,将使用相同的密钥接收它。试试这个代码。

主要活动 2

super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        txtUrl=findViewById(R.id.url);
        txtPhoneNum=findViewById(R.id.phoneNum);
        button0=findViewById(R.id.goback);
        Intent passedIntent= getIntent();
        if(passedIntent != null){
            Bundle mybundle= passedIntent.getBundleExtra("Sender");
            ArrayList list= mybundle.getStringArrayList("list");
            Toast.makeText(getApplicationContext(), "Student info: "
                    +list.get(0)+list.get(1)+list.get(2),duration).show();
        }
        String Url=txtUrl.getText().toString();
        String PhoneNum=txtPhoneNum.getText().toString();

不相关但有用的东西


不要尝试查看 toast 中的值。我在这里看到了:

Toast.makeText(getApplicationContext(), "Student info: "
                    +list.get(0)+list.get(1)+list.get(2),duration).show();

尝试调试或在日志中显示。