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数据传值再获取的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android什么是函数,什么是函数响应
- 下一篇: android 直播 app下载地址,蓝