EditText使用SetText时出现空指针异常
代码如下:
package com.sings.tushu;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by Administrator on 2016/7/14.
*/
public class StudentDetail extends ActionBarActivity implements View.OnClickListener{
private Button btnsave,btndelete,btnclose;
private EditText edname,edage,edemail;
private int _student_id=0;
Student student;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
btnsave= (Button) findViewById(R.id.btnSave);
btnsave.setOnClickListener(this);
btnclose= (Button) findViewById(R.id.btnClose);
btnclose.setOnClickListener(this);
btndelete= (Button) findViewById(R.id.btnDelete);
btndelete.setOnClickListener(this);
edname= (EditText) findViewById(R.id.edName);
edname.setText(student.name);
edage= (EditText) findViewById(R.id.edAge);
edage.setText(String.valueOf(student.age));
edemail= (EditText) findViewById(R.id.edEmail);
edemail.setText(student.email);
_student_id=0;
Intent intent = getIntent();
_student_id = intent.getIntExtra("student_Id",0);
StudentRepo repo = new StudentRepo(this);
student = repo.getStudentById(_student_id);
}
@Override
public void onClick(View v) {
if (v==findViewById(R.id.btnSave)){
StudentRepo repo = new StudentRepo(this);
Student student = new Student();
student.age=Integer.parseInt(edage.getText().toString());
student.email=edemail.getText().toString();
student.name=edname.getText().toString();
student.student_Id=_student_id;
if (_student_id==0){
_student_id=repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}else{
repo.update(student);
Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show();
}
}else if (v==findViewById(R.id.btnDelete)){
StudentRepo repo = new StudentRepo(this);
repo.delete(_student_id);
Toast.makeText(this,"Student Record Deleted",Toast.LENGTH_SHORT).show();
finish();
}else if (v==findViewById(R.id.btnClose)){
finish();
}
}
}
红字位置就是出现空指针的地方
搞了一天没弄好,麻烦大神帮忙看看,谢谢!!
是能进入界面的,但一按添加按钮,就出错了,以下是LOG
07-15 21:55:04.398 4395-4395/com.sings.tushu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sings.tushu, PID: 4395
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sings.tushu/com.sings.tushu.StudentDetail}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference
at com.sings.tushu.StudentDetail.onCreate(StudentDetail.java:37)
at android.app.Activity.performCreate(Activity.java:6262)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)