为什么我的图片没有在视图寻呼机中加载Picasso库?

时间:2016-06-20 09:19:32

标签: java android android-fragments android-studio

嗨,开发人员我试图使用Picasso库在视图寻呼机中加载图像,但是它的所有内容都会让我看到空的查看器,而不是我的代码会发生什么

这是我的片段代码

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

    Bundle bundle = this.getArguments();
    String id = bundle.getString("id");

    dish_images_array = new ArrayList<>();
    new get_dishes_details_data().execute(getString(R.string.dish_details)+id);

    pager = (ViewPager)v.findViewById(R.id.img_slider);
    pager.setAdapter(new slider_images(getActivity(), dish_images_array));
    return v;
}

这是我的slider_images.java适配器代码

package com.example.yasee.bitescene.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.yasee.bitescene.R;
import com.squareup.picasso.Picasso;
import java.util.List;

public class slider_images extends PagerAdapter {
    Context c;
    private List<String> _imagePaths;
    private LayoutInflater inflater;
    ImageView imgDisplay;

public slider_images(Activity c, List<String> imagePaths) {
    this._imagePaths = imagePaths;
    this.c = c;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == (object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {


    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.slider, container, false);
    imgDisplay = (ImageView) viewLayout.findViewById(R.id.slider_image);
    Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
    (container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    (container).removeView((RelativeLayout) object);
}
}

这是我的错误

  

06-20 05:12:05.095 3280-3280 / com.example.yasee.bitescene E / InputEventReceiver:异常调度输入事件。   06-20 05:12:05.095 3280-3280 / com.example.yasee.bitescene E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback   06-20 05:12:05.115 3280-3280 / com.example.yasee.bitescene E / MessageQueue-JNI:java.lang.IllegalStateException:应用程序的PagerAdapter在不调用PagerAdapter的情况下更改了适配器的内容# notifyDataSetChanged!预期的适配器项目数:0,找到:3寻呼机ID:com.example.yasee.bitescene:id / img_slider寻呼机类:类android.support.v4.view.ViewPager有问题的适配器:类com.example.yasee.bitescene.adapter .slider_images                                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1071)                                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1025)                                                                                    在android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2114)                                                                                    在android.view.View.dispatchTouchEvent(View.java:7706)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)                                                                                    在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)                                                                                    在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)                                                                                    在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)                                                                                    在android.app.Activity.dispatchTouchEvent(Activity.java:2458)                                                                                    在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)                                                                                    在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)                                                                                    在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2016)                                                                                    在android.view.View.dispatchPointerEvent(View.java:7886)                                                                                    在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)                                                                                    在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)                                                                                    在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3399)                                                                                    在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3449)                                                                                    在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3418)                                                                                    在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3525)                                                                                    在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3426)                                                                                    在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3582)                                                                                    在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3399)                                                                                    在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3449)                                                                                    在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3418)                                                                                   在android.view.ViewRootImpl $ InputStage.apply(查看   06-20 05:12:05.125 3280-3280 / com.example.yasee.bitescene E / AndroidRuntime:FATAL EXCEPTION:main                                                                              处理:com.example.yasee.bitescene,PID:3280                                                                              java.lang.IllegalStateException:应用程序的PagerAdapter在不调用PagerAdapter#notifyDataSetChanged的情况下更改了适配器的内容!预期的适配器项目数:0,找到:3寻呼机ID:com.example.yasee.bitescene:id / img_slider寻呼机类:类android.support.v4.view.ViewPager有问题的适配器:类com.example.yasee.bitescene.adapter .slider_images

当我尝试滑动图片时,android监视器中出现上述错误..

这是我的结果。

enter image description here

这是我的AsyncTask任务代码

private class get_dishes_details_data extends AsyncTask<String, Void, Boolean> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Loading... please wait");
        dialog.show();
        dialog.setCancelable(false);
    }


    @Override
    protected Boolean doInBackground(String... urls) {
        try {
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("dish_details");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject jsonrealobject = jarray.getJSONObject(i);
                    dishname = jsonrealobject.getString("dish_name");
                    restaurantname = jsonrealobject.getString("name");
                    businesses_id = jsonrealobject.getString("businesses_id");
                }
                JSONArray imagesarry = jsono.getJSONArray("dish_images");
                for (int i = 0; i < imagesarry.length(); i++){
                    JSONObject jobject = imagesarry.getJSONObject(i);
                    String image = jobject.getString("dish_image");
                    dish_images_array.add(image);
                }
                return true;
            }

        } catch (ParseException | IOException | JSONException e1) {
            e1.printStackTrace();
        }
        return false;
    }
    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        textView_dish_name = (TextView)getActivity().findViewById(R.id.textView_dish_name);
        textView_dish_name.setText(dishname);
        textView_dish_name.setTypeface(custom_font);

        restaurent_name = (TextView)getActivity().findViewById(R.id.restaurent_name);
        restaurent_name.setText(restaurantname);
        restaurent_name.setTypeface(custom_font);

        yelp_id = (TextView)getActivity().findViewById(R.id.yelp_id);
        yelp_id.setText("Link To The Restaurant: https://www.yelp.com/biz/"+businesses_id);
        yelp_id.setTypeface(custom_font);
        if(result == false){
            Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

预期的适配器项目数:0,找到:3 意味着您设置适配器的时间早于页面项目,并且明确表示您没有调用pageAdapter.notifyDataSetChanged()

我建议您查看PagerAdapter格式的notifyDataSetChanged(),并使其类似。查看在那里调用root.setCenter的位置。

答案 1 :(得分:0)

这个问题与毕加索无关。根据你的日志,我认为你正在更新arryalist但不调用notifyDataSetChanged()。

PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 3 

现在根据你的代码,我的建议是改变几行 -

        slider_images sliderImagesAdapter; // At class level

        pager = (ViewPager)v.findViewById(R.id.img_slider);
        sliderImagesAdapter = new slider_images(getActivity(), dish_images_array)
        pager.setAdapter(sliderImagesAdapter);

这里为slider_images创建一个类级变量。在onPostExecute中, 这样做,

    if(result)
      sliderImagesAdapter.notifyDataSetChanged();

每次在提供给任何适配器的列表中进行任何更改(添加/删除)时调用notifyDataSetChanged()

希望它会对你有所帮助:)。

答案 2 :(得分:0)

如果notifyDataSetChanged()无效,您可以重新初始化 适配器

pager.setAdapter(new slider_images(getActivity(), dish_images_array));