java.lang.NullPointerException: println needs a message
我的代码:
public class ViewMupuh extends Activity implements OnClickListener {
private static String position = null;
private TextView textJudul, textLirik;
private Button bUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_mupuh);
Intent intent = getIntent();
position = intent.getStringExtra("position");
Log.d("value of position",position);
DBHelper db = new DBHelper(this);
Lirik lirik = db.getLirik(position);
textJudul = (TextView) findViewById(R.id.judul_details);
textJudul.setText(lirik.getJudul());
textLirik = (TextView) findViewById(R.id.lirikdetails);
textLirik.setText(lirik.getLirik());
bUpdate = (Button) findViewById(R.id.bupdatedetails);
bUpdate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), EditLirik.class);
intent.putExtra("position value", position);
startActivity(intent);
}
}
感谢
答案 0 :(得分:1)
这是因为position = intent.getStringExtra("position");
返回null
而Log.d("value of position",position);
引发此异常。
尝试更改
Log.d("value of position",position);
有关
Log.d("value of position", "" + position); // If position is null will log "null"
或者
if (position != null) {
Log.d("value of position", position);
}
无论如何,请考虑在代码的其余部分position
之后intent.getStringExtra("position");
可以为空。