码迷,mamicode.com
首页 > 其他好文 > 详细

Activity回传值报错:Failure delivering result ResultInfo{who=null,request=7,result = 0,data=null}

时间:2016-08-24 01:06:59      阅读:517      评论:0      收藏:0      [点我收藏+]

标签:

Activity  A   -----值------->  Activity  B   -----值----->  Activity  A

    场景:当A跳转到B,再从B直接点击返回按钮时,报错。
package com.itheima.smsSender;
 
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
 
/**
*
* 短信发送器: 三个界面 --- 三个Activity --- listview
*
*/
 
public class MainActivity extends Activity implements OnClickListener {
 
private EditText et_number;
private EditText et_content;
private Button bt_select;
private Button bt_sendSms;
private Button bt_quickReply;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件
et_number = (EditText) findViewById(R.id.et_number);
et_content = (EditText) findViewById(R.id.et_content);
bt_select = (Button) findViewById(R.id.bt_select);
bt_sendSms = (Button) findViewById(R.id.bt_sendSms);
bt_quickReply = (Button) findViewById(R.id.bt_quickReply);
 
// 添加点击事件
bt_select.setOnClickListener(this);
bt_sendSms.setOnClickListener(this);
bt_quickReply.setOnClickListener(this);
}
 
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
 
switch (id) {
case R.id.bt_select:
Intent contact_intent = new Intent(getApplicationContext(), ContactActivity.class);
startActivityForResult(contact_intent, 7);
break;
case R.id.bt_sendSms:
SmsManager manager = SmsManager.getDefault();
String destinationAddress = et_number.getText().toString().trim();
String data = et_content.getText().toString().trim();
manager.sendTextMessage(destinationAddress , null, data , null, null);
break;
case R.id.bt_quickReply:
Intent quick_intent = new Intent(getApplicationContext(), QuickReplyActivity.class);
startActivityForResult(quick_intent, 8);
break;
}
 
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 7:
String number = data.getStringExtra("number");
et_number.setText(number);
break;
case 8:
String sms = data.getStringExtra("quickInfo");
et_content.setText(3);
break;
}
}
}

 

技术分享
 
错误信息:
技术分享
    MainActivity程序73行报空指针异常。
String number = data.getStringExtra("number");
错误分析:
package com.itheima.smsSender;
 
import java.util.ArrayList;
 
import com.itheima.smsSender.adapter.MyAdapter;
import com.itheima.smsSender.bean.Person;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
 
public class ContactActivity extends Activity {
ArrayList<Person> persons = new ArrayList<Person>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//加载布局文件
setContentView(R.layout.activity_contact);
//初始化数据 --- 改进用内容提供者获取联系人
for(int i=0;i<30;i++){
Person person = new Person("person"+i,"1854678925"+i);
persons.add(person);
}
//获取控件
ListView lv_contact = (ListView) findViewById(R.id.lv_contact);
//添加适配器
lv_contact.setAdapter(new MyAdapter(persons,this));
//为适配器添加点击事件
lv_contact.setOnItemClickListener(new OnItemClickListener() {
 
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("number", persons.get(position).phone);
setResult(1, intent);
finish();
}
});
}
}

 

当B直接点击返回键返回到A时,未走上面34行的点击事假,所以返回的Intent为初始值null,所以在走MainActivity的73行代码回报空指针异常。requestCode一开始就被复制了,所以一定会匹配case。 解决办法:
方法①:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data == null) { //判断数据是否为空,就可以解决这个问题
return;
} else {
switch (requestCode) {
case 7:
String number = data.getStringExtra("number");
et_number.setText(number);
break;
case 8:
String sms = data.getStringExtra("quickInfo");
et_content.setText(3);
break;
}
}
}

 

 
方法②:由于没有走点击事件里的代码,所以resultCode没有被赋值,仍然为初始值0,所以不会走case:1或case:3,就不会报错了。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
String number = data.getStringExtra("number");
et_number.setText(number);
break;
case 3:
String sms = data.getStringExtra("quickInfo");
et_content.setText(3);
break;
}
}
}

Activity回传值报错:Failure delivering result ResultInfo{who=null,request=7,result = 0,data=null}

标签:

原文地址:http://www.cnblogs.com/aimqqroad-13/p/5801332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!