2014-10-04 54 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或不使用它。

相關問題