初学,这段代码也是在网上找的
开发选用的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();
}
}
你上面的程式碼是沒問題的,所以你確定你的username與password賦了值?
如果賦了值那就不是這裡的問題了 你把程式碼都貼出來吧。
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); Intent intent1 = new Intent(Layout_1.this, Layout_2.class);
startActivity(intent1);
從程式碼看是layout_2裡的bundle為null導致的,要看你這段程式碼是在哪裡執行的,可能當時沒有拿到bundle資料呢