日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/10/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android传输注册数据异常,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();

}

}

總結

以上是生活随笔為你收集整理的android传输注册数据异常,android数据传值再获取的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。