[英]Why does this throw a 'null object reference' error?
I'm trying to pass a string from another activity to this one and then send it into an array and then into a listview. 我试图将字符串从另一个活动传递到此活动,然后将其发送到数组,然后发送到列表视图。 whenever i run this i get "Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference" it seems like the error has something to do with the fourth line with the 'extras' bundle but how is that a null object reference i defined it right there no?
每当我运行此命令时,我都会收到“尝试在null对象引用上调用虚拟方法'android.os.Bundle android.content.Intent.getExtras()'”的错误,这似乎与第四行带有“ Extras的捆绑包,但是我没有定义的空对象引用又如何呢?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
switch(requestCode) {
case ACTIVITY_EDIT:
String title = extras.getString(add.TITLE);
String password = extras.getString(add.PASSWORD);
adapter.add(title);
break;
}
}
Here's where the 'PASSWORD' and 'TITLE' variables are defined in the other Activity: 这是另一个活动中定义的“ PASSWORD”和“ TITLE”变量的位置:
public void onClick(View v) {
EditText titleBox = (EditText)findViewById(R.id.titleText);
TITLE = titleBox.getText().toString();
EditText passBox = (EditText)findViewById(R.id.passwdText);
String pass = passBox.getText().toString();
EditText confBox = (EditText)findViewById(R.id.editText3);
String conf = confBox.getText().toString();
if (pass.equals(conf)) {
PASSWORD = pass;
this.finish();
} else {
Toast.makeText(this, "Passwords don't match", Toast.LENGTH_SHORT);
}
}
When you pass data from one activity to another using a Bundle, the data is received inside onCreate()
method of the second activity not inside onActivityResult()
unless you've specifically implemented that. 当您使用Bundle将数据从一个活动传递到另一个活动时,数据将在第二个活动的
onCreate()
方法内部接收,而不是在onActivityResult()
内部,除非您已专门实现。
Check this answer on how to start another activity and how to pass data to another activity : https://stackoverflow.com/a/20170125/1239966 检查有关如何启动另一个活动以及如何将数据传递到另一个活动的以下答案: https : //stackoverflow.com/a/20170125/1239966
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.