大神前身-小白 2015-09-01 15:25 采纳率: 100%
浏览 10373
已采纳

Android Intent 如何接收到指定的Intent传递过来的值呢?

写了2个Activity,A、B,2个Activity之间可以相互跳转。
1、A中有一个发送按钮可实现传值到B中,同时还有一个独立的跳转按钮。
2、在B中有接收A传值过来的的代码
3、执行中,如果直接执行了跳转按钮,B的接收Intent代码中,就会出现空指针异常,目前通过 try{}catch临时确保能正常运行。
想请问:能否有方法可以让B接收传值的代码能够识别那个Intent才是传值过来的,避免空指针异常出现。

A传值代码如下:
/*
* 1、获取到界面中输入的信息
* 2、将获取到的值,传递到ReceiveActivity中。
* */
Button _OkSend = (Button)findViewById(R.id.buttonOkSend);
_OkSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText1 = (EditText)findViewById(R.id.editText);
name=editText1.getText().toString();
Log.i("YuryLog","SendActivity name: "+name);

            EditText editText2 = (EditText)findViewById(R.id.editText2);
            eatSomething = editText2.getText().toString();
            Log.i("YuryLog","SendActivity eatSomething: "+eatSomething);

            EditText editText3 = (EditText)findViewById(R.id.editText3);
            copies = editText3.getText().toString();
            Log.i("YuryLog","SendActivity copies: "+copies);

// 将值传递到ReceiveActivity中
Intent _intent = new Intent(SendActivity.this, ReceiveActivity.class) ;
_intent.putExtra("sendName",name);
_intent.putExtra("sendeatSomething",eatSomething);
_intent.putExtra("sendcopies",copies);

           startActivity(_intent);
        }
    });

B接收代码如下:
// 接收SendActivity中传递过来的值;
// 需要确定,只有SendActivity中传递过来的值才能触发,否则第一次执行的时候会出现空指针异常;
try {
Intent _getIntent = this.getIntent();
if( _getIntent.getExtras().getString("sendName") != null){
Log.i("YuryLog","理论上只有点了确认键才执行");
receiveName = _getIntent.getExtras().getString("sendName");
receiveEatSomething = _getIntent.getExtras().getString("sendeatSomething");
receiveCopies = _getIntent.getExtras().getString("sendcopies");

            Log.i("YuryLog",receiveName + receiveEatSomething + receiveCopies);
            //        在界面中显示接收到的值
            //        获取到TextView的id,再动态的更改它的值
            TextView _textView =(TextView) findViewById(R.id.textView4);
            TextView _textView2 =(TextView) findViewById(R.id.textView5);

            _textView.setText(receiveName+": ");
            _textView2.setText("来" + receiveCopies + "份" + receiveEatSomething);
        }
    }catch (Exception e){
        e.printStackTrace();
    }

万分感谢各位能提供一个思路,谢谢。

  • 写回答

3条回答

  • 盼汕 2015-09-02 01:19
    关注

    首先,尽量不要用try{}catch去捕捉能用判断规避的异常,那样会影响效率,每次出现异常,虚拟机要抓错误调用堆栈。所以,最好的方式是通过判断去规避。
    按你的思路,可以先判断getIntent.getExtras()是否为null。
    Intent _getIntent = this.getIntent();
    if( _getIntent.getExtras() != null){
    Log.i("YuryLog","理论上只有点了确认键才执行");
    receiveName = _getIntent.getExtras().getString("sendName");
    receiveEatSomething = _getIntent.getExtras().getString("sendeatSomething");
    receiveCopies = _getIntent.getExtras().getString("sendcopies");
    ......

    要指出的是,上述代码,最好使用getXXXExtra这类方法,它不会出现空指针(除了少数几个,比方说getStringExtra)。
    需要设定默认值的,在没有值时它会返回默认值;没有设置默认值的,在没有值时会返回null,针对这类判空一下。
    可以看下getBooleanExtra的源码:
    public boolean getBooleanExtra(String name, boolean defaultValue) {
    return mExtras == null ? defaultValue :
    mExtras.getBoolean(name, defaultValue);
    }

    而getExtras()在没有值时会返回null,看下源码:
    public Bundle getExtras() {
    return (mExtras != null)
    ? new Bundle(mExtras)
    : null;
    }

    所以,最好不要用getIntent().getExtras()这种方式,换用getIntent().getXXXExtras(),这样针对有设置默认值的就不需要判空了。

    activity之间传值,是没有机制可以确定哪个activity传过来的。这是考虑到代码的可扩展性,解耦。要确定哪个activity发过来,在intent创建那里多传个布尔值就行,比方说下面的代码。
    发送
    intent.putExtra("fromXXActivity", true);

        接收
        if (getIntent().getBooleanExtra("fromXXActivity", false)) {
            ......
                        // 这里,你就可以安全的接收那个activity发过来的所有值。
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记