使用向其他活动发送意图有什么问题?

发布于 2025-01-20 15:58:57 字数 2352 浏览 1 评论 0原文

我可以发送带有arraylist的捆绑包,但是我的代码导致nullpointerexception。但是我不知道为什么。

主要活动

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时。

I what to send a bundle with an ArrayList, but my code causes a NullPointerException. But I don't know why.

Main 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(receive here)

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();
}

Here, which part is wrong? Arraylist is added to the bundle but every time a NullPointerException is thrown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2025-01-27 15:58:57

您正在传递钥匙的问题


,然后尝试使用另一个密钥来获取值。这是错误的。要清除您的理解,让我给您一个例子→

案例:您的商店里有很多T恤。您订购了一些T恤出售,并获得RedGreen Tee恤。现在,当一个自定义来了,他问Yellow T恤时,您会说它不存在。在编码术语中称为null。这是您在这里的同一情况。让我们检查解决方案。

解决方案


时,当您使用密钥发送值时,请使用相同的密钥接收它。只需尝试此代码即可。

MainActivity 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.makeText(getApplicationContext(), "Student info: "
                    +list.get(0)+list.get(1)+list.get(2),duration).show();

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

The issue


You are passing a key and then trying to get the value with another key. This is wrong. To clear your understanding, let me give you an example →

Case: You have many tee-shirts in your shop. You ordered some tee-shirts to sale and got red and green tee-shirts only. Now, when a custom comes and he asks yellow tee-shirts, then you say that it is not there. That is called null in coding terms. This is the same case with you over here. Let's check the solution.

The solution


When you send a value with a key, receive it with same key. Just try this code.

MainActivity 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();

The not related but helpful thing


Do not try to see the values in toast. I see that here:

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

Try to debug or display it in log instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文