Java 未能传递结果 ResultInfo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20782619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failure delivering result ResultInfo
提问by timeshift117
There are many people who have encountered the same error on stackoverflow, but I haven't been able to find any relevant resolution in those posts. My MainActivity is starting a new activity (SecondActivity) with startActivityForResult(); SecondActivity then returns some data with onBackPressed(); and putExtra();
有很多人在stackoverflow上遇到过同样的错误,但是我在这些帖子中找不到任何相关的解决方案。我的 MainActivity 正在使用 startActivityForResult() 开始一个新活动 (SecondActivity);SecondActivity 然后用 onBackPressed() 返回一些数据;和 putExtra();
Extract from MainActivity.java:
从 MainActivity.java 中提取:
public void addNewNote(View v){
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Source", "NEW");
startActivityForResult(intent, 1); //1 is the result code
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("TAG", data.getStringExtra("Note"));
if (requestCode == 1) {
if(resultCode == RESULT_OK){
listItems.add(data.getStringExtra("Note"));
Log.v("TAG", data.getStringExtra("Note"));
adapter.notifyDataSetChanged();
listView.invalidateViews();
}
if (resultCode == RESULT_CANCELED) {
}
}
}
Extract from SecondActivity.java:
摘自 SecondActivity.java:
@Override
public void onBackPressed() {
super.onBackPressed();
if (mainTextField.getText() != null){
Intent returnIntent = new Intent();
returnIntent.putExtra("Note",mainTextField.getText());
setResult(RESULT_OK, returnIntent);
finish();
} else {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
}
Logcat:
日志猫:
采纳答案by amit singh
Try this-
尝试这个-
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1 && data != null)
{
Log.v("TAG", data.getStringExtra("Note"));
if(resultCode == RESULT_OK)
{
listItems.add(data.getStringExtra("Note"));
Log.v("TAG", data.getStringExtra("Note"));
adapter.notifyDataSetChanged();
listView.invalidateViews();
}
if (resultCode == RESULT_CANCELED)
{
}
}
}
回答by gigo
The problem with me was that I used getIntent(), thus getting the intent of the current activity I was on.
我的问题是我使用了getIntent(),从而获得了我正在进行的当前活动的意图。
When I switched to data.getStringExtra()it worked. Amateur mistake...
当我切换到data.getStringExtra() 时它起作用了。业余失误...