无法启动活动ComponentInfo {example。}:java.lang.NullPointerException:println需要一条消息

时间:2015-05-11 15:38:58

标签: android

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);
    }
}

感谢

1 个答案:

答案 0 :(得分:1)

这是因为position = intent.getStringExtra("position");返回nullLog.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");可以为空。