我的数据为空且未设置

时间:2018-04-14 11:44:32

标签: android fragment

我在日志中看到以下错误。我现在问题是mContact未设置并且为null。我被困住了,整个晚上一直在努力。

  

java.lang.NullPointerException:尝试调用虚方法   “java.lang.String中   com.bignerdranch.android.adressbook.Contact.getName()'上的null   对象参考                                                                                            在   com.bignerdranch.android.adressbook.ContactFragment.onCreateView(ContactFragment.java:80)                                                                                            在   android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)                                                                                            在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)                                                                                            在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)                                                                                            在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)                                                                                            在   android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)                                                                                            在   android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)                                                                                            在   android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)                                                                                            在   android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)                                                                                            在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)

,失败的行是ContactFragment

中的第80行

我有以下代码的代码

import static android.widget.CompoundButton.*;

public class ContactFragment extends Fragment {

    private static final String ARG_CRIME_ID = "crime_id";
    private Contact mContact;//to hold the views current crime
    private TextView nameTextView; // displays contact's name
    private TextView phoneTextView; // displays contact's phone
    private TextView emailTextView; // displays contact's email
    private TextView streetTextView; // displays contact's street
    private TextView cityTextView; // displays contact's city
    private TextView stateTextView; // displays contact's state
    private TextView zipTextView; // displays contact's zip
    private Callbacks mCallbacks;

    /**
     * Required interface for hosting activities.
     */
    public interface Callbacks {
        void onContactUpdated(Contact contact);
    }


    public static ContactFragment newInstance(UUID crimeId) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_CRIME_ID, crimeId);

        ContactFragment fragment = new ContactFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UUID contactId =  (UUID) getArguments().getSerializable(ARG_CRIME_ID);
        mContact = ContactLab.get(getActivity()).getContact(contactId);
        Log.d("m","oncreate of contactFragment");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);

        // get the EditTexts
        nameTextView = (TextView) view.findViewById(R.id.nameTextView);
        phoneTextView = (TextView) view.findViewById(R.id.phoneTextView);
        emailTextView = (TextView) view.findViewById(R.id.emailTextView);
        streetTextView = (TextView) view.findViewById(R.id.streetTextView);
        cityTextView = (TextView) view.findViewById(R.id.cityTextView);
        stateTextView = (TextView) view.findViewById(R.id.stateTextView);
        zipTextView = (TextView) view.findViewById(R.id.zipTextView);
       nameTextView.setText(mContact.getName());


        return view;
    }

    @Override
    public void onPause() {
        super.onPause();

        ContactLab.get(getActivity())
                .updateContact(mContact);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

    }






    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_details_menu, menu);
    }

    // handle menu item selections
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_edit:

                return true;
            case R.id.action_delete:
                UUID contacId=(UUID) getArguments().getSerializable(ARG_CRIME_ID);
                Contact contact =ContactLab.get(getActivity()).getContact(contacId);
                ContactLab.get(getActivity()).deleteContact(contact);
                getActivity().finish();
                return true;

        }

        return true;
    }
}               

我需要知道我的错误在哪里,因为我无法将textView设置为值。

三江源

1 个答案:

答案 0 :(得分:0)

您正面临此异常,因为mContact为null。您必须创建联系对象,而不是任何问题。

在将值设置为textview

之前将其添加到onCreateView
linked_list