使用getText()。toString()时的NullPointer异常.trim();

时间:2014-12-10 03:42:49

标签: java android nullpointerexception

我有以下课程:

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:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:text="Name"
                android:textColor="@color/action_bar"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editContactName"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/border"
                android:ems="10"
                android:hint="Name"
                android:imeOptions="actionNext"
                android:inputType="textPersonName"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:text="Description"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/action_bar"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editContactDescription"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/border"
                android:ems="10"
                android:hint="Description"
                android:imeOptions="actionNext"
                android:inputType="textShortMessage"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:textSize="30sp" />
                        <TextView
                android:id="@+id/numbers"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="center"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="15dp"
                android:text="Primary and Secondary Numbers"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/action_bar"
                android:textSize="25sp" />

            <RelativeLayout
                android:id="@+id/NumberFields"
                android:layout_width="fill_parent"
                android:layout_height="50dp" >

                <EditText
                    android:id="@+id/editContactPrimaryNumber"
                    android:layout_width="250dp"
                    android:layout_height="50dp"
                    android:layout_marginLeft="35dp"
                    android:layout_weight="2"
                    android:background="@drawable/border"
                    android:ems="10"
                    android:gravity="center_vertical|center_horizontal"
                    android:hint="Primary"
                    android:inputType="phone"
                    android:paddingLeft="30dp"
                    android:paddingRight="30dp"
                    android:textSize="30sp" />

                <EditText
                    android:id="@+id/editContactSecondaryNumber"
                    android:layout_width="250dp"
                    android:layout_height="50dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="25dp"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:ems="10"
                    android:gravity="center_vertical|center_horizontal"
                    android:hint="Secondary"
                    android:inputType="phone"
                    android:paddingLeft="30dp"
                    android:paddingRight="30dp"
                    android:textSize="30sp" />

            </RelativeLayout>

            <TextView
                android:id="@+id/email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:text="Email"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/action_bar"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editContactEmail"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/border"
                android:ems="10"
                android:hint="Email"
                android:imeOptions="actionNext"
                android:inputType="textEmailAddress"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/skype"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:text="Skype ID"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/action_bar"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editContactSkype"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/border"
                android:ems="10"
                android:hint="Skype ID"
                android:imeOptions="actionNext"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:textSize="30sp" />
            <Button
                android:id="@+id/addContactButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="30dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/sh_button_selector"

                android:padding="@dimen/padding_medium"
                android:text="Save Contact"
                android:textColor="@color/text_white"
                android:textSize="@dimen/text_size_take_survey_button"
                android:textStyle="bold" />
       </LinearLayout>
   </ScrollView>


</LinearLayout>

现在当我运行我的应用程序时,我在onClick中的onCreateView()中得到一个NullPointerException:

String newName = name.getText().toString().trim();

现在我已经尝试打印出变量,调试但我似乎无法弄清楚为什么会出现NullPointerException。这可能是一件简单的事情,我忽略了,我已经看了很长时间,我可能只是疯了。

4 个答案:

答案 0 :(得分:5)

我认为您的EditText'名称'未初始化。

答案 1 :(得分:2)

您尚未初始化EditText,因此您获得了NPE。首先初始化这些组件..通过代码调用setupTextFieldsByContact()中的onCreate方法,它将解决您的问题。

答案 2 :(得分:1)

当然你得到一个NullPointerException ......你从未在onCreate方法中初始化你的EditTexts。将此添加到您的onCreate:

name = (EditText) findViewById(R.id.editContactName);
description = (EditText) findViewById(R.id.editContactDescription);
primaryNumber = (EditText) findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) findViewById(R.id.editContactSecondaryNumber);
email = (EditText) findViewById(R.id.editContactEmail);
skype = (EditText) findViewById(R.id.editContactSkype);

这样你就可以在实际存在的对象上调用方法。

答案 3 :(得分:0)

您的EditText&#39;名称&#39;未初始化。你必须首先初始化你的EditText