Code is as follows:
package com.happy;
import com.person.PersonBean;
import com.service.PersonService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class addActivity extends Activity {
private EditText name;
private EditText age;
private EditText phone;
private Button save;
private static final String TAG = "addActivity";
protected void onCreate (Bundle savedInstanceState) {
/ / TODO Auto-generated method stub
super.onCreate (savedInstanceState);
setContentView (R.layout.activity00);
age = (EditText) findViewById (R.id.addActivity_age);
name = (EditText) findViewById (R.id.addActivity_name);
phone = (EditText) findViewById (R.id.addActivity_phone);
save = (Button) findViewById (R.id.save_but);
save.setOnClickListener (new SaveOnClickListener ());
}
private class SaveOnClickListener implements OnClickListener {
PersonService service = new PersonService (addActivity.this);
private PersonBean person;
@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
String myName = name.getText (). toString ();
Log.i (TAG, myName);
int myAge = Integer.valueOf (age.getText (). toString ());
String myPhone = phone.getText (). toString ();
person.setName (myName);
person.setAge (myAge);
person.setPhone (myPhone);
service.save (person);
Toast.makeText (addActivity.this, "saved successfully", 2000). show ();
}
}
}
/ / given as follows: person.setName (myName); null pointer
I log query results can be obtained to myName, why set the pointer to personbean it will be empty
------ Solution ------------------------------------ --------
private PersonBean person; declare that you just are not initialized, the default is null, so you tune person.setName (myName);
be wrong
------ Solution ---------------------------------- ----------
do you think the person is not a null pointer you do?
you really did not see a person in instantiation statements.
没有评论:
发表评论