android传输注册数据异常,android数据传值再获取的问题

博主初学Android开发,选用SDK 4.0和ADT 5.0,测试手机为4.4.4。在代码中进行数据传递和获取时,运行出现'java.lang.NullPointerException'错误提示及'XXX已停止运行'提示,但点击确定后程序能继续运行且可获取变量数据,博主询问如何处理该问题并给出全部代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

开发选用的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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值