空指针异常背后的原因是什么?

时间:2016-02-08 13:25:46

标签: android android-asynctask android-viewpager bitmapfactory android-pageradapter

  

我正在使用寻呼机适配器在视图寻呼机中显示图像,这些图像将由asynctask中的bitmapfactory下载。

pageradapter类:

public class pageradapter extends PagerAdapter {
    Button load_img;
    ImageView imgview;
    Bitmap bitmap;



    Context mContext;
    LayoutInflater mLayoutInflater;
    List<String> l = MainActivity.list;
    ImageLoader mImageLoader;

    public pageradapter(Context context) {
        mContext = context;


    }

    @Override
    public int getCount() {
        return 4;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageLoader mImageLoader = ImageLoader.getInstance();
        mLayoutInflater = ((LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        View view = mLayoutInflater.inflate(R.layout.img, container, false);
          imgview = (ImageView) view.findViewById(R.id.imageView3);

Bitmap btp=new LoadImage().doInBackground(l.get(position));
        imgview.setImageBitmap(btp);

        container.addView(view);

        return view;
    }

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

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

    public class LoadImage extends AsyncTask<String, String, Bitmap> {

        ProgressDialog pDialog = new ProgressDialog(mContext);
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog.setMessage("Loading Image ....");
            pDialog.show();

        }
        protected Bitmap doInBackground(String... args) {

            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());

            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap image) {

            if(image != null){
                imgview.setImageBitmap(image);
                pDialog.dismiss();

            }else{

                pDialog.dismiss();
                Toast.makeText(mContext, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();

            }
        }
    }

}
  

我的logcat:

     

02-08 18:49:22.554 18345-18345 / autogenie.dg10 E / AndroidRuntime:FATAL   例外:主要                                                                   显示java.lang.NullPointerException                                                                       at autogenie.dg10.pageradapter $ LoadImage.doInBackground(pageradapter.java:89)                                                                       at autogenie.dg10.pageradapter.instantiateItem(pageradapter.java:54)                                                                       在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)                                                                       在android.support.v4.view.ViewPager.populate(ViewPager.java:1020)                                                                       在android.support.v4.view.ViewPager.populate(ViewPager.java:952)                                                                       在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:681)                                                                       在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)                                                                       在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                       在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)                                                                       在android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)                                                                       在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)                                                                       在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)                                                                       在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)                                                                       在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)                                                                       在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                       在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2176)                                                                       在android.view.View.measure(View.java:15518)                                                                       在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)                                                                       在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)                                                                       在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)                                                                       在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)                                                                       在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4351)                                                                       在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)                                                                       在android.view.Choreographer.doCallbacks(Choreographer.java:562)                                                                       在android.view.Choreographer.doFrame(Choreographer.java:532)                                                                       在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)                                                                       在android.os.Handler.handleCallback(Handler.java:725)                                                                       在android.os.Handler.dispatchMessage(Handler.java:92)                                                                       在android.os.Looper.loop(Looper.java:137)                                                                       在android.app.ActivityThread.main(ActivityThread.java:5041)                                                                       at java.lang.reflect.Method.invokeNative(Native Method)                                                                       在java.lang.reflect.Method.invoke(Method.java:511)                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)                                                                       在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

调用AsyncTask的错误方法。您不直接致电doInBackground()

更改

Bitmap btp=new LoadImage().doInBackground(l.get(position));

Bitmap btp = new LoadImage().execute(l.get(position));