EditText使用SetText时出现空指针异常

qq_29954515 qq_29954515 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
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) 
...全文

相关推荐

android-tip-calculator

android-tip-calculator

沈临白

Android无数据库版个人信息注册:Activity与Intent传值入门实战

Android开发入门阶段,掌握Activity生命周期、Intent页面跳转与控件事件监听是构建应用的基础能力。在UI交互层面,EditText、RadioGroup、CheckBox等表单控件的数据获取与传递,构成了绝大多数业务页面的核心逻辑。通过一个不依赖数据库的个人信息注册案例,开发者可以专注理解界面搭建、数据封装、Intent传值以及RecyclerView列表展示的完整链路。这种轻量级实现方式降低了学习曲线,让初学者先跑通内存数据流,再逐步过渡到SharedPreferences或Room持久化

weixin_34348174

Android Studio无数据库版个人信息注册页面:从界面搭建到SharedPreferences持久化实操

在Android开发学习路径中,注册与登录功能是绕不开的实战起点。很多新手一接触注册页面就被SQLite、Room等数据库概念劝退,却忽略了表单界面搭建、输入校验、数据封装与页面状态传递这些更基础且核心的技术环节。实际上,数据存储只是功能链路中的一环,而SharedPreferences作为一种轻量级键值对持久化方案,非常适合保存用户偏好、登录状态和小体积结构化数据。理解它的工作原理,即XML文件读写与异步提交机制,能帮助开发者快速实现“记住上次注册信息”这类典型应用场景。本文以Android Studio

weixin_34006965

Android中EditText setText方法的踩坑实战

主要给大家分享了一些关于Android中EditText setText方法的踩坑记录,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38530202

android kotlin ktx包

Android 下 kotlin 的ktx 工具包,直接以moudle形式导入使用

十一个11
评论 12
收藏 已收藏
分享
返回顶部
qq_29954515
Android 80,489 成员 91,371 内容