2014-10-04 87 views
0

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

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) 
+0

显然,以下情况之一是正确的:'mDateButton'为'null','mCrime'为'null',或'mCrime.getDate()'为'null'。请延长你的答案。 – Smutje 2014-10-04 23:40:16

+0

这是你的问题在哪里你的问题是'com.bignerdranch.android.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:55)'在你的CrimeFragment中的第55行,无论你正在访问什么都没有实例化它,请看看它。 – NightwareSystems 2014-10-05 04:05:40

回答

0

o f mCrime永远不会设置,所以当你尝试调用一个永远不会实例化的变量的方法时,它会抛出一个空指针异常。

mCrime.getDate().toString() 

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

相关问题