使用getText()时出现java空指针异常。toString()。修剪();
我有以下课程:
public class SHConfigureContactFragment extends Fragment{
private EditText name;
private EditText description;
private EditText primaryNumber;
private EditText secondaryNumber;
private EditText email;
private EditText skype;
private Byte[] photo;
private Boolean isDualPane;
private FragmentManager fragmentManager;
private SHContactMenuFragment menuFragment;
private SHConfigureContactFragment contactFragment;
private DatabaseControllerLibrary controller;
private Contact contact;
private SHPatient patient;
private int patientId;
public View rootView;
public int selectedIndex;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null) {
this.selectedIndex = savedInstanceState.getInt("SELECTED_INDEX");
this.contact = (Contact) savedInstanceState.getSerializable("CONTACT");
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("CONTACT", contact);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controller = SHController.getInstance(getActivity());
patient = ((SHController) controller).getPatient();
patientId = patient.getId();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(R.layout.sh_fragment_contact_edit, container, false);
Button button = (Button) rootView.findViewById(R.id.addContactButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
boolean isValid = true;
// assigning IDs based on how many contacts are created. no reused IDs son!
int id = controller.getContactsCount();
id++;
String newName = name.getText().toString().trim();
String newDescription = description.getText().toString().trim();
String newPrimaryNumber = primaryNumber.getText().toString().trim();
String newSecondaryNumber = secondaryNumber.getText().toString().trim();
String newEmail = email.getText().toString().trim();
String newSkype = skype.getText().toString().trim();
if(newName.length()==0 || newName.matches(".*\\d.*")) {
name.setText("");
isValid = false;
}
if(newPrimaryNumber.length()==0) {
primaryNumber.setText("");
isValid = false;
}
if (isValid){
Contact updatedContact;
ContactConfiguration updatedConfiguration;
if (contact != null){
id = contact.getId();
}
updatedContact = new Contact(id, newName, newDescription, newPrimaryNumber, newSecondaryNumber, newEmail, newSkype, null);
updatedConfiguration = new ContactConfiguration(patientId, patientId, false, updatedContact);
if (contact == null){
controller.addContact(updatedContact, patientId);
}
else{
controller.updateContact(updatedContact, patientId);
}
}
}
});
return rootView;
}
private void setupTextFieldsByContact(Contact contact) {
name = (EditText) rootView.findViewById(R.id.editContactName);
description = (EditText) rootView.findViewById(R.id.editContactDescription);
primaryNumber = (EditText) rootView.findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) rootView.findViewById(R.id.editContactSecondaryNumber);
email = (EditText) rootView.findViewById(R.id.editContactEmail);
skype = (EditText) rootView.findViewById(R.id.editContactSkype);
if (contact != null) {
name.setText(contact.getName());
description.setText(contact.getDescription());
primaryNumber.setText(contact.getPrimaryNumber());
secondaryNumber.setText(contact.getSecondaryNumber());
email.setText(contact.getEmail());
skype.setText(contact.getSkype());
}
}
我有相应的XML文件用于布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >
<ScrollView
安卓:id="@+id/scrollView1"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" >
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@color/background"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/name"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="5dp"
安卓:layout_marginLeft="30dp"
安卓:layout_marginTop="15dp"
安卓:text="Name"
安卓:textColor="@color/action_bar"
安卓:textSize="25sp" />
<EditText
安卓:id="@+id/editContactName"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:hint="Name"
安卓:imeOptions="actionNext"
安卓:inputType="textPersonName"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
<TextView
安卓:id="@+id/description"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="5dp"
安卓:layout_marginLeft="30dp"
安卓:layout_marginTop="15dp"
安卓:text="Description"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:textColor="@color/action_bar"
安卓:textSize="25sp" />
<EditText
安卓:id="@+id/editContactDescription"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:hint="Description"
安卓:imeOptions="actionNext"
安卓:inputType="textShortMessage"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
<TextView
安卓:id="@+id/numbers"
安卓:layout_width="wrap_content"
安卓:layout_height="30dp"
安卓:layout_gravity="center"
安卓:layout_marginBottom="5dp"
安卓:layout_marginTop="15dp"
安卓:text="Primary and Secondary Numbers"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:textColor="@color/action_bar"
安卓:textSize="25sp" />
<RelativeLayout
安卓:id="@+id/NumberFields"
安卓:layout_width="fill_parent"
安卓:layout_height="50dp" >
<EditText
安卓:id="@+id/editContactPrimaryNumber"
安卓:layout_width="250dp"
安卓:layout_height="50dp"
安卓:layout_marginLeft="35dp"
安卓:layout_weight="2"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:gravity="center_vertical|center_horizontal"
安卓:hint="Primary"
安卓:inputType="phone"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
<EditText
安卓:id="@+id/editContactSecondaryNumber"
安卓:layout_width="250dp"
安卓:layout_height="50dp"
安卓:layout_alignParentRight="true"
安卓:layout_alignParentTop="true"
安卓:layout_marginRight="25dp"
安卓:layout_weight="1"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:gravity="center_vertical|center_horizontal"
安卓:hint="Secondary"
安卓:inputType="phone"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
</RelativeLayout>
<TextView
安卓:id="@+id/email"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="5dp"
安卓:layout_marginLeft="30dp"
安卓:layout_marginTop="15dp"
安卓:text="Email"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:textColor="@color/action_bar"
安卓:textSize="25sp" />
<EditText
安卓:id="@+id/editContactEmail"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:hint="Email"
安卓:imeOptions="actionNext"
安卓:inputType="textEmailAddress"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
<TextView
安卓:id="@+id/skype"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="5dp"
安卓:layout_marginLeft="30dp"
安卓:layout_marginTop="15dp"
安卓:text="Skype ID"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:textColor="@color/action_bar"
安卓:textSize="25sp" />
<EditText
安卓:id="@+id/editContactSkype"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:background="@drawable/border"
安卓:ems="10"
安卓:hint="Skype ID"
安卓:imeOptions="actionNext"
安卓:paddingLeft="30dp"
安卓:paddingRight="30dp"
安卓:textSize="30sp" />
<Button
安卓:id="@+id/addContactButton"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"
安卓:layout_marginTop="30dp"
安卓:layout_alignParentBottom="true"
安卓:layout_centerHorizontal="true"
安卓:background="@drawable/sh_button_selector"
安卓:padding="@dimen/padding_medium"
安卓:text="Save Contact"
安卓:textColor="@color/text_white"
安卓:textSize="@dimen/text_size_take_survey_button"
安卓:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
现在,当我运行我的应用程序时,我在以下行的onClick中的onCreateView()中得到一个NullPointerException:
String newName = name.getText().toString().trim();
现在我已经试着打印出变量,进行调试,但我似乎不明白为什么会出现NullPointerException。这可能是我忽略的一些简单的事情,我只是看了这么久,我可能会发疯
# 1 楼答案
我想你的
EditText
“name”没有初始化# 2 楼答案
您的EditText“name”未初始化。你必须先初始化编辑文本
# 3 楼答案
当然你会得到一个NullPointerException。。。您从未在onCreate方法中初始化EditText。将以下内容添加到onCreate中:
这样,您就可以对实际存在的对象调用方法
# 4 楼答案
您还没有初始化这些
EditText
,所以您得到了NPE。首先初始化这些组件。。在onCreate
中,从代码调用这个方法setupTextFieldsByContact()
,它将解决您的问题