cari

Rumah  >  Soal Jawab  >  teks badan

android-studio - android数据传值再获取的问题

初学,这段代码也是在网上找的

开发选用的SDK是4.0,ADT是5.0,测试手机是4.4.4

在Layout_1中的传递代码

Intent intentSimple = new Intent();
intentSimple.setClass(Layout_1.this,Layout_2.class);

Bundle bundleSimple = new Bundle();
bundleSimple.putString("username", username);
bundleSimple.putString("password",password);
intentSimple.putExtras(bundleSimple);
startActivity(intentSimple);

在Layout_2中的获取代码

   Bundle bundle = this.getIntent().getExtras();
   username = bundle.getString("username");
   password = bundle.getString("password");

运行会出现这个错误提示“ java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference”

运行会出现一个错误提示“很抱歉,XXX已停止运行”,虽然出现这个错误提示,点击“确定”之后程序会继续运行,变量数据也能获取到

请问如何处理?

全部代码如下

Layout_1

package com.hl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class Layout_1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_1);


        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                TextView usernameView = (TextView) findViewById(R.id.username);
                String username = usernameView.getText().toString().trim();
                TextView passwordView = (TextView) findViewById(R.id.password);
                String password = passwordView.getText().toString().trim();

                Intent intentSimple = new Intent();
                intentSimple.setClass(Layout_1.this,Layout_2.class);

                Bundle bundleSimple = new Bundle();
                bundleSimple.putString("username", username);
                bundleSimple.putString("password",password);
                intentSimple.putExtras(bundleSimple);
                startActivity(intentSimple);


                Intent intent1 = new Intent(Layout_1.this, Layout_2.class);
                startActivity(intent1);

            }
        });

        }


}

Layout_2

package com.hl;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;


public class Layout_2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_2);

        Bundle bundle = this.getIntent().getExtras();
        String username = bundle.getString("username");
        String password = bundle.getString("password");
        Toast.makeText(Layout_2.this, username + "  " + password, Toast.LENGTH_SHORT).show();

    }

}
PHP中文网PHP中文网2772 hari yang lalu618

membalas semua(3)saya akan balas

  • 黄舟

    黄舟2017-04-17 16:16:27

    你上面的代码是没问题的,所以你确定你的username与password赋了值?
    如果赋了值那就不是这里的问题了 你把代码都贴出来吧。

    balas
    0
  • 阿神

    阿神2017-04-17 16:16:27

    startactivity函数调用了两次,第一次调用时有值传递,第二次传递没有值
    Intent intentSimple = new Intent();
    intentSimple.setClass(Layout_1.this,Layout_2.class);

    Bundle bundleSimple = new Bundle();
    bundleSimple.putString("username", username);
    bundleSimple.putString("password",password);
    intentSimple.putExtras(bundleSimple);
    startActivity(intentSimple);
    Intent intent1 = new Intent(Layout_1.this, Layout_2.class);
    startActivity(intent1);

    balas
    0
  • ringa_lee

    ringa_lee2017-04-17 16:16:27

    从代码看是layout_2里的bundle为null导致的,要看你这段代码是在哪里执行的,可能当时没有拿到bundle数据呢

    balas
    0
  • Batalbalas