在片段上使用setter时出现NullPointerException

时间:2014-12-09 04:48:04

标签: java android android-listfragment getter-setter

我一直在尝试调试这一段时间,并且我无法弄清楚为什么我在尝试使用片段上的对象的setter时获得NullPointerException。

片段代码:

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class ExerciseFragment extends ListFragment {

    private ExerciseDetails mExercise;
    private ArrayList<NewSet> mSets;
    private String mName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


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

        Bundle b = getArguments();
        mName = b.getString("NAME");
        mExercise.setName(mName);
        getActivity().setTitle(mName);
        ListView lv = (ListView) rootView.findViewById(android.R.id.list);



        return rootView;
    }


}

包含我需要使用的setter的类:

import java.util.ArrayList;
import java.util.Date;

public class ExerciseDetails {


    private String mName;
    private Date mDate;
    private ArrayList<NewSet> mSets;

    public ExerciseDetails() {

        mName = "";
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public Date getDate() {
        return mDate;
    }

    public void setDate(Date date) {
        mDate = date;
    }


    public ArrayList<NewSet> getSets() {
        return mSets;
    }

    public void setSets(ArrayList<NewSet> sets) {
        mSets = sets;
    }

}

这是描述nullpointerexception的行:

mExercise.setName(mName);

错误跟踪:

12-08 23:34:27.610: E/AndroidRuntime(791): Process: com.test.project, PID: 791
12-08 23:34:27.610: E/AndroidRuntime(791): java.lang.NullPointerException
12-08 23:34:27.610: E/AndroidRuntime(791):  at com.test.project.ExerciseFragment.onCreateView(ExerciseFragment.java:33)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.os.Handler.handleCallback(Handler.java:733)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.os.Handler.dispatchMessage(Handler.java:95)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.os.Looper.loop(Looper.java:137)
12-08 23:34:27.610: E/AndroidRuntime(791):  at android.app.ActivityThread.main(ActivityThread.java:4998)
12-08 23:34:27.610: E/AndroidRuntime(791):  at java.lang.reflect.Method.invokeNative(Native Method)
12-08 23:34:27.610: E/AndroidRuntime(791):  at java.lang.reflect.Method.invoke(Method.java:515)
12-08 23:34:27.610: E/AndroidRuntime(791):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-08 23:34:27.610: E/AndroidRuntime(791):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-08 23:34:27.610: E/AndroidRuntime(791):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

mExercise为NULL 。通过创建新对象来初始化它:

mExercise=new ExerciseDetails();
mExercise.setName(mName);

答案 1 :(得分:1)

练习是NULL。

将其初始化为

ExerciseDetails mExercise=new ExerciseDetails();

然后使用

mExercise.setName(mName);