繁体   English   中英

不断获取java.lang.NullPointerException,但不知道为什么或如何解决它

[英]Keep getting java.lang.NullPointerException but don't know why or how to fix it

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

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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM