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);
}
}
谢谢
发布于 2015-05-11 15:47:41
这是因为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");
之后为null。
https://stackoverflow.com/questions/30171934
复制相似问题