将json响应变为null

时间:2017-04-04 12:34:52

标签: android json

在下面的代码中,我从json响应中获取imageUrl为null。因此应用程序崩溃了。这是代码 的片段

public class ReviewFragment extends BaseFragment {
private ListView mListView;
private ViewSwitcher mEmptyView;
private ReviewAdapter mReviewAdapter;
private List<Review> reviews;

@Override
protected String getScreenName() {
    return "Reviews";
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_my_review, null);
    enableToolbar(true, view, getString(R.string.label_my_reviews));
    mReviewAdapter = new ReviewAdapter(getContext(), R.layout.view_review_list_item);

    mListView = (ListView) view.findViewById(R.id.list);
    mEmptyView = (ViewSwitcher) view.findViewById(R.id.empty);
    getLoaderManager().initLoader(1, null, mMyReviewCallback);
    mListView.setAdapter(mReviewAdapter);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

private LoaderManager.LoaderCallbacks<List<Review>> mMyReviewCallback =
        new LoaderManager.LoaderCallbacks<List<Review>>() {
            @Override
            public Loader<List<Review>> onCreateLoader(int id, Bundle args) {
                mEmptyView.setDisplayedChild(0);
                mEmptyView.setVisibility(View.VISIBLE);
                return new Review.MyReviewLoader(getActivity());
            }

            @Override
            public void onLoadFinished(Loader<List<Review>> loader, List<Review> data) {

                updateReviews(data);
            }

            @Override
            public void onLoaderReset(Loader<List<Review>> loader) {

            }
        };

private void updateReviews(List<Review> reviewList) {
    if (reviewList.size() > 0) {
        mEmptyView.setVisibility(View.GONE);
        mReviewAdapter.clear();
        this.reviews = reviewList;
        if (reviews.size() == 0) {
            mEmptyView.setDisplayedChild(1);
            mEmptyView.setVisibility(View.VISIBLE);
        } else {

            mReviewAdapter.clear();
            mReviewAdapter.addAll(reviews);
        }
    } else {
        mEmptyView.setDisplayedChild(1);
        mEmptyView.setVisibility(View.VISIBLE);
    }


}}

适配器

class ReviewAdapter extends ArrayAdapter<Review> {

private static final String TAG = "ReviewAdapter";
private final int mResourceId;

ReviewAdapter(Context context, int resource) {
    super(context, resource);
    mResourceId = resource;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    if (convertView == null) {
        convertView = View.inflate(getContext(), mResourceId, null);
        holder = new Holder(convertView);
    } else {
        holder = (Holder) convertView.getTag();
    }

    bindView(holder, position);
    return convertView;
}

private void bindView(Holder holder, int position) {
    final Review review = getItem(position);
    Uri imageUri = review.getImageUri(Review.IMAGE_SQ,Beco.getApp().getDensity());

    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(imageUri)
            .setProgressiveRenderingEnabled(true)
            .build();
    DraweeController controller = Fresco.newDraweeControllerBuilder()
            .setImageRequest(request)
            .setOldController(holder.storeLogo.getController())
            .build();
    GenericDraweeHierarchyBuilder builder =
            new GenericDraweeHierarchyBuilder(getContext().getResources());
    GenericDraweeHierarchy hierarchy = builder
            .setFadeDuration(200)
            .setPlaceholderImage(ContextCompat.getDrawable(getContext(), R.drawable.image_deal_default))
            .build();

    holder.storeLogo.setHierarchy(hierarchy);
    holder.storeLogo.setController(controller);
    holder.background.setBackgroundColor(review.getColor());
    holder.storeName.setText(review.getName());
    holder.postedOn.setText(review.getCommentedOn());
    holder.comment.setText(review.getComments());
    holder.rating.setRating(review.getRating());
}


private class Holder {
    SimpleDraweeView storeLogo;
    FrameLayout background;
    TextView storeName;
    TextView postedOn;
    RatingBar rating;
    TextView comment;

    Holder(View convertView) {
        convertView.setTag(this);
        storeLogo = (SimpleDraweeView) convertView.findViewById(R.id.store_logo);
        storeName = (TextView) convertView.findViewById(R.id.store_name);
        postedOn = (TextView) convertView.findViewById(R.id.posted_on);
        comment = (TextView) convertView.findViewById(R.id.comment);
        rating = (RatingBar) convertView.findViewById(R.id.rating);
        background = (FrameLayout) convertView.findViewById(R.id.store_background);
    }
}}

模型类

public class Review {
public static final String IMAGE_SQ = "_sq";
private static final String DEFAULT_STORE_COLOR = "FFFFFFFF";
private String user;
private String[] imageUrl;
private String name;
private int rating;
private String comments;
private String commentedOn;
private String color;

public String getUser() {
    return user;
}

imageUrl[1]));
    }
    return null;
}

@ColorInt
public int getColor() {
    if (color.length() < 6)
        color = DEFAULT_STORE_COLOR;

    String colorString = color.substring(0, 6);
    String alphaString = color.substring(6);

    if (alphaString.length() == 0) {
        alphaString = "FF";
    }

    int colorValue = Integer.parseInt(colorString, 16);
    int alphaValue = Integer.parseInt(alphaString, 16);
    return Color.argb(alphaValue, Color.red(colorValue), Color.green(colorValue), Color.blue(colorValue));
}

public void setUser(String user) {
    this.user = user;
}

public int getRating() {
    return rating;
}

public void setRating(int rating) {
            this.rating = rating;
}

public String getComments() {
    return comments;
}

public void setComments(String comments) {
    this.comments = comments;
}

public String getCommentedOn() {
    return commentedOn;
}

public void setCommentedOn(String commentedOn) {
    this.commentedOn = commentedOn;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public static class MyReviewLoader extends ModelListLoader<Review> {

    public MyReviewLoader(Context context) {
        super(context);

    }

    @Override
    public List<Review> fetch() {
        return BecoApi.getClient().getReviews();
    }
}}

这是 stacktrace

FATAL EXCEPTION: main
                                                          Process: com.beco.ibeco, PID: 9950
                                                          java.lang.NullPointerException
                                                              at com.facebook.common.internal.Preconditions.checkNotNull(Preconditions.java:210)
                                                              at com.facebook.imagepipeline.request.ImageRequestBuilder.setSource(ImageRequestBuilder.java:110)
                                                              at com.facebook.imagepipeline.request.ImageRequestBuilder.newBuilderWithSource(ImageRequestBuilder.java:53)
                                                              at com.beco.ibeco.app.account.ReviewAdapter.bindView(ReviewAdapter.java:56)
                                                              at com.beco.ibeco.app.account.ReviewAdapter.getView(ReviewAdapter.java:48)
                                                              at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                              at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
                                                              at android.widget.ListView.onMeasure(ListView.java:1188)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
                                                              at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:767)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                              at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2645)
                                                              at android.view.View.measure(View.java:18811)
                                                              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
                                                              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                            at android.

从这个Adapter类中,调试期间获取的Uri imageUri的值为null?请帮助我

1 个答案:

答案 0 :(得分:1)

尝试将String[] imageUrl;的字段更改为ArrayList<String> imageUrl = new ArrayList<>();