使用向其他活动发送意图有什么问题?
我可以发送带有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在传递钥匙的问题
,然后尝试使用另一个密钥来获取值。这是错误的。要清除您的理解,让我给您一个例子→
案例:您的商店里有很多T恤。您订购了一些T恤出售,并获得
Red
和Green
Tee恤。现在,当一个自定义来了,他问Yellow
T恤时,您会说它不存在。在编码术语中称为null
。这是您在这里的同一情况。让我们检查解决方案。解决方案
时,当您使用密钥发送值时,请使用相同的密钥接收它。只需尝试此代码即可。
MainActivity 2
无关但有用的事情
尝试查看吐司中的值。我在这里看到:
尝试调试或在日志中显示。
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
andgreen
tee-shirts only. Now, when a custom comes and he asksyellow
tee-shirts, then you say that it is not there. That is callednull
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
The not related but helpful thing
Do not try to see the values in toast. I see that here:
Try to debug or display it in log instead.