继续获取java.lang.NullPointerException,但不要';我不知道为什么或如何修复它

继续获取java.lang.NullPointerException,但不要';我不知道为什么或如何修复它,java,android,nullpointerexception,android-studio,Java,Android,Nullpointerexception,Android Studio,在这两个类中,我不断得到一个空指针异常错误。我已经缩小到一行,但我不知道为什么或如何修复它 mDateButton.setText(mCrime.getDate().toString()); 下面是调用此行的类: package com.bignerdranch.android.criminalintent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.Editab

在这两个类中,我不断得到一个空指针异常错误。我已经缩小到一行,但我不知道为什么或如何修复它

mDateButton.setText(mCrime.getDate().toString());
下面是调用此行的类:

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

/**
 * Created by tidalwave on 9/11/14.
 */
public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField; //initializes variable for listener
    //Initialize widget instance for mDateButton variable
    private Button mDateButton;
    //Initialize widget instance for mSolvedCheckBox variable
    private CheckBox mSolvedCheckBox;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);
        // Wiring up the EditText widget for the fragment
        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.addTextChangedListener(new TextWatcher() {

            //show or store items once you start typing
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mCrime.setTitle(s.toString());

            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //this space left blank on purpose
            }


            public void afterTextChanged(Editable s) {
                //This space left blank on purpose
            }
        });
        //Setup text for mDateButton


        mDateButton = (Button)v.findViewById(R.id.crime_date);//get a reference for the new button
        mDateButton.setText(mCrime.getDate().toString());//set its text as the date of the crime
        mDateButton.setEnabled(false);//Disable the usage of date button by setting it to false
        // Disabling the button ensures that it will not respond in any way to the user pressing it
        //it also greys out the button so you know not to click on it

        //Setup listener of the crime solved checkbox (mSolvedCheckbox)
        mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);//get a reference for the new checkbox
        //assign mSolvedCheckBox a value once someone clicks on the checkbox
        //OnCheckedChangedListener is a part of the CompoundButton Class
        //Checkbox is a sub-class of CompoundButton
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            //@Override are not required for methods defined in interfaces
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //Set the crime's solved property
                mCrime.setSolved(isChecked);
            }
        });
        return v;

    }




}
以下是该行正在调用的类:

  package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

/**
 * Created by tidalwave on 9/11/14.
 */
public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField; //initializes variable for listener
    //Initialize widget instance for mDateButton variable
    private Button mDateButton;
    //Initialize widget instance for mSolvedCheckBox variable
    private CheckBox mSolvedCheckBox;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);
        // Wiring up the EditText widget for the fragment
        mTitleField = (EditText)v.findViewById(R.id.crime_title);
        mTitleField.addTextChangedListener(new TextWatcher() {

            //show or store items once you start typing
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mCrime.setTitle(s.toString());

            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //this space left blank on purpose
            }


            public void afterTextChanged(Editable s) {
                //This space left blank on purpose
            }
        });
        //Setup text for mDateButton


        mDateButton = (Button)v.findViewById(R.id.crime_date);//get a reference for the new button
        mDateButton.setText(mCrime.getDate().toString());//set its text as the date of the crime
        mDateButton.setEnabled(false);//Disable the usage of date button by setting it to false
        // Disabling the button ensures that it will not respond in any way to the user pressing it
        //it also greys out the button so you know not to click on it

        //Setup listener of the crime solved checkbox (mSolvedCheckbox)
        mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);//get a reference for the new checkbox
        //assign mSolvedCheckBox a value once someone clicks on the checkbox
        //OnCheckedChangedListener is a part of the CompoundButton Class
        //Checkbox is a sub-class of CompoundButton
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            //@Override are not required for methods defined in interfaces
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //Set the crime's solved property
                mCrime.setSolved(isChecked);
            }
        });
        return v;

    }




}
下面是错误日志:

1498-1498/com.bignerdranch.android.criminalintent E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bignerdranch.android.criminalintent, PID: 1498
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch.android.criminalintent.CrimeActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.bignerdranch.android.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:55)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
        at android.app.Activity.performStart(Activity.java:5241)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)

mCrime的值从未设置,因此当您尝试为从未实例化的变量调用方法时,它将抛出空指针异常

mCrime.getDate().toString()

解决方案是实例化mcinite或不使用它。

显然,以下情况之一是正确的:
mDateButton
null
mcinite
null
,或
mcinite.getDate()
null
。请扩展您的答案。这就是您的问题所在
com.bignerdranch.android.criminantent.CrimeFragment.onCreateView(CrimeFragment.java:55)
CrimeFragment中的第55行,无论您访问的是什么,都没有实例化它,请查看它。