我的代码中有什么错误,显示"空指针异常"

时间:2014-08-10 16:26:52

标签: android nullpointerexception

我是android新手。我正在尝试从用户和吐司中获取数据到屏幕...但我面临空指针异常......我正在尝试从很长一段时间来解决... plzzz帮助我 这是我的代码:

 public class MainActivity extends ActionBarActivity {

    private EditText etName;
    private EditText etEmail;
    private EditText etPhone;
    private EditText etDesignation;
    DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         findViewById(R.id.etName);
         findViewById(R.id.etEmail);
         findViewById(R.id.etPhone);
         findViewById(R.id.etDesignation);
        //dbHelper = new DatabaseHelper(this);
    }

    public void save(View v) {
        String name = etName.getText().toString();
        String email = etEmail.getText().toString();
        String phone = etPhone.getText().toString();
        String designation = etDesignation.getText().toString();

        Employee employee = new Employee(name, email, phone, designation);
        Toast.makeText(getApplicationContext(), employee.toString(),
                Toast.LENGTH_LONG).show();



        }

5 个答案:

答案 0 :(得分:0)

您的字段未初始化。只做一个findViewById(R.id.etName);是不够的,因为全局成员将持有null值给出NullPointerException。因此,分配很重要

private EditText etName;         // default - null
private EditText etEmail;        // default - null
private EditText etPhone;        // default - null
private EditText etDesignation;  // default - null
DatabaseHelper dbHelper;         // default - null

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     // changes here, link layout fields to objects

     etName = (EditText) findViewById(R.id.etName);
     etEmail = (EditText)findViewById(R.id.etEmail);
     etPhone = (EditText)findViewById(R.id.etPhone);
     etDesignation = (EditText)findViewById(R.id.etDesignation);
    //dbHelper = new DatabaseHelper(this);
}

public void save(View v) {
    String name = etName.getText().toString();
    String email = etEmail.getText().toString();
    String phone = etPhone.getText().toString();
    String designation = etDesignation.getText().toString();

    Employee employee = new Employee(name, email, phone, designation);
    Toast.makeText(getApplicationContext(), employee.toString(),
            Toast.LENGTH_LONG).show();



    }

答案 1 :(得分:0)

试试这个

private EditText etName;
private EditText etEmail;
private EditText etPhone;
private EditText etDesignation;
DatabaseHelper dbHelper;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;
    etName = (EditText)findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etDesignation = (EditText) findViewById(R.id.etDesignation);
    //dbHelper = new DatabaseHelper(this);
}

public void save(View v) {
    String name = etName.getText().toString();
    String email = etEmail.getText().toString();
    String phone = etPhone.getText().toString();
    String designation = etDesignation.getText().toString();

    Employee employee = new Employee(name, email, phone, designation);
    Toast.makeText(context, employee.toString(),
            Toast.LENGTH_LONG).show();



    }

答案 2 :(得分:0)

在onCreate

 etName = (EditText)findViewById(R.id.etName);
 etEmail = (EditText)findViewById(R.id.etEmail);
 etPhone = (EditText)findViewById(R.id.etPhone);
 etDesignation = (EditText)findViewById(R.id.etDesignation);

您忘记初始化视图,并在null

上调用了getText()

答案 3 :(得分:0)

我建议你先查看基础知识

替换这部分:

etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etPhone = (EditText) findViewById(R.id.etPhone);
    etDesignation = (EditText) findViewById(R.id.etDesignation);
    dbHelper = new DatabaseHelper(this);

答案 4 :(得分:0)

看来您的字段未被实例化,这通常表示空指针异常。您是否尝试过更改

 findViewById(R.id.etName);
 findViewById(R.id.etEmail);
 findViewById(R.id.etPhone);
 findViewById(R.id.etDesignation);

 this.eName = (EditText)findViewById(R.id.etName);
 this.etEmail = (EditText)findViewById(R.id.etEmail);
 this.etPhone = (EditText)findViewById(R.id.etPhone);
 this.etDesignation = (EditText)findViewById(R.id.etDesignation);  

?你是否也确定所有如果R.id.是有效的参考?