使用putExtra()将UUID传递给另一个活动后,将UUID分配给对象

时间:2014-07-12 22:36:16

标签: android nullpointerexception uuid

我似乎无法将UUID分配给对象。我将UUID从一个活动传递到另一个活动--UUID正确传递。但我现在无法在没有获得空指针异常的情况下将其分配给对象

第一项活动:

package com.example.superfriends;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class DesginNewSFFragment extends Fragment {

private ImageView male, female;
public SuperFriend mSF;

private final String TAG = "DesignNewSFFragment";
private final String IS_MALE = "IS MALE";
private final static String EXTRA_SF_ID = "com.example.superfriends.sf_id";

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

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_design_new_sf, parent, false);
    getActivity().setTitle(R.string.design_SF);

    male = (ImageView)v.findViewById(R.id.male);
    male.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mSF = new SuperFriend(true);
            Intent i = new Intent(getActivity(), ChooseMaleCostumeActivity.class);
            i.putExtra(DesginNewSFFragment.EXTRA_SF_ID, mSF.getID());
            Log.d(TAG, "UUID initial: " + mSF.getID());
            startActivity(i);

        }
    });

    female = (ImageView)v.findViewById(R.id.female);
    female.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mSF = new SuperFriend(false);
            Intent i = new Intent(getActivity(), ChooseFemaleCostumeActivity.class);
            i.putExtra(DesginNewSFFragment.EXTRA_SF_ID, mSF.getID());
            Log.d(TAG, "Design new female SF");
            startActivity(i);

        }
    });

    return v;
}

}

第二项活动:

import java.util.UUID;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ChooseFemaleCostumeFragment extends Fragment {

int selected;
SuperFriend mSF;
int[] femaleCostumes;
private ImageView i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11;
private Button next;

private static final String EXTRA_SF_ID = "com.example.superfriends.sf_id";
private final String TAG = "ChooseFemaleCostumeFragment";

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

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_new_male_female_sf, parent, false);
    getActivity().setTitle(R.string.choose_a_costume);

    Intent i = getActivity().getIntent();
    Bundle extras = i.getExtras();
    if (extras != null) {
        UUID sfId = (UUID)i.getSerializableExtra(EXTRA_SF_ID);
        Log.d(TAG, "UUID final: " + sfId);
        mSF = AllMySF.get(getActivity()).getSF(sfId);
        Log.d(TAG, "mSF assigned to: " + mSF.getID());    //NULL POINTER EXCEPTION HERE WHEN mSF.getID() IS USED

    } }

包含方法getSF()的类:

package com.example.superfriends;
import java.util.ArrayList;
import java.util.UUID;
import android.content.Context;

public class AllMySF {

private static AllMySF sAllMySF;
private ArrayList<SuperFriend> mMySFList;
private Context mAppContext;

private AllMySF (Context appContext) {
    mAppContext = appContext;
    mMySFList = new ArrayList<SuperFriend>();

    //to generate a false list
    for (int i=0; i<10; i++) {
        int isMale = i%2;
        SuperFriend s = new SuperFriend(isMale==0, "SF #" + i);
        mMySFList.add(s);
    }
}

public static AllMySF get(Context c) {
    if (sAllMySF == null) sAllMySF = new AllMySF(c.getApplicationContext());
    return sAllMySF;
}

public ArrayList<SuperFriend> getSFs() {
    return mMySFList;
}

public SuperFriend getSF (UUID id) {
    for (SuperFriend s: mMySFList) {
        if (s.getID().equals(id)) return s;
    }
    return null;
}

}

包含getID()的SuperFriend类:

package com.example.superfriends;
import java.util.UUID;

public class SuperFriend {

private UUID mId;
private String mName;
private boolean mMale;
private int mHair;
private int mCostume;
private int mMask;
private int mPicId;

public SuperFriend(boolean isMale) {
    mId = UUID.randomUUID();
    mMale = isMale;
    mPicId = R.drawable.woman;
}
public UUID getID() {
    return mId;
}
}

任何人都可以解释为什么我不能将UUID分配给对象mSF?

1 个答案:

答案 0 :(得分:0)

所以我最终弄明白了,我这是多么愚蠢的错误:P我没有创建一种方法将新的SuperFriend添加到AllMySF中当前的SuperFriends列表中。一旦我这样做,新的SuperFriend就在SuperFriends的数据库中,我可以在不引起异常的情况下调用它。