我已经制作了代码并且没有错误,但 logcat 显示NullPointerException
。这是我传递和捕获每个活动的值的代码。请帮我解决这个问题。
第一项活动:
try {
Intent in = new Intent();
in.setClass(p_daftar_pelanggan.this, ubah_data_pelanggan.class);
in.putExtra("id", eid.getText().toString());
in.putExtra("nama", tnama.getText().toString());
in.putExtra("alamat", ealamat.getText().toString());
in.putExtra("no_hp", ehp.getText().toString());
startActivity(in);
}
catch(NullPointerException ex)
{
}
第二项活动:
Bundle b = getIntent().getExtras();
String idpgu = b.getString("id");
String namapgu = b.getString("nama");
String alamatpgu = b.getString("alamat");
String hppgu = b.getString("no_hp");
idp.setText(idpgu);
idp.setKeyListener(null);
nama.setText(namapgu);
ealamat.setText(alamatpgu);
hp.setText(hppgu);
答案 0 :(得分:0)
在第二项活动中尝试以下内容:
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
答案 1 :(得分:0)
你在FirstActivity中使用Intent添加额外内容但是使用Bundle来接收它,这就是为什么你有NullPointerException,因为SecondActivity正在寻找不同位置的值。 在第二个实践中执行以下操作
Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
String namapgu = intent.getStringExtra("nama");
String alamatpgu = intent.getStringExtra("alamat");
String hppgu = intent.getStringExtra("no_hp");
// Check Null for each Values e.g.
if (idpgu.trim().equals(" "))
{ idpgu = "null";
}
答案 2 :(得分:0)
您可以将Bundle用于此目的:
Activity destActivity = new Activty();
Bundle b = new Bundle();
destActivity.putString("TAG_ForValue",YourValue.getText().toString());
destActivity.setArguments(b);
Intent intent = new Intent(startActivity.this, destActivity.class);
startActivity(intent);
在你的destActivity中:
Bundle bundle = getArguments();
if(bundle !=null)
String value = bundle.getString("TAG_ForValue");
答案 3 :(得分:0)
You are sending and receiving the values correctly, but probably some values are null, you can validate setting an empty string instead of a null value:
Bundle b = getIntent().getExtras();
String idpgu = (b.getString("id") != null)? b.getString("id") : "";
String namapgu = (b.getString("nama") != null)? b.getString("nama") : "";
String alamatpgu = (b.getString("alamat") != null)? b.getString("alamat") : "";
String hppgu = (b.getString("no_hp") != null)? b.getString("no_hp") : "";
答案 4 :(得分:0)
@YRO:
请检查您的buffer
声明。你的try catch block
声明是什么。你的catch
阻止是空的,这就是catch
抛出的原因。
因此,首先,您可以检查如何从Java文档中处理NullPointerException
块。
我希望它会对你有所帮助。