执行可运行时的空指针异常

用户名

我在两行中收到空指针异常。这是我在代码中使用注释指定的。我已经启动了所有变量并实现了所有方法,但仍然无法解决。请让我知道为什么它显示空指针异常?

 public class PhotoActivity extends Activity implements SetPhotos, OnGestureListener{
 private static final float SWIPE_THRESHOLD = 100;

   private static final float SWIPE_VELOCITY_THRESHOLD = 100;

 ArrayList<String> url = new ArrayList<String>();

    ImageView iv;
      @SuppressLint("UseSparseArrays")
 HashMap<Integer, Bitmap> imagemap = new HashMap<Integer,Bitmap>();

  Handler handler = new Handler();
    String mode;
     int i=0;
      @SuppressWarnings("deprecation")
    GestureDetector swipe = new GestureDetector(this);
         private LruCache<Integer, Bitmap> cache;
     final int max = (int) (Runtime.getRuntime().maxMemory()/1024);
    final int cachesize = max/8;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);
    cache = new LruCache<>(max);
    iv = (ImageView)findViewById(R.id.imageView1);

    if(getIntent().getExtras()!= null){
        this.url = getIntent().getStringArrayListExtra("msg");
        if(getIntent().getExtras().containsKey(MainActivity.PHOTO)){

            mode = getIntent().getStringExtra(MainActivity.PHOTO);
            new Image(i,this).execute(url.get(i));//error null pointer exception
        }
        else if (getIntent().getExtras().containsKey(MainActivity.SLIDE)){
    mode = getIntent().getStringExtra(MainActivity.SLIDE);
    h.run();//error null pointer exception
        }
    }
}
Runnable h = new Runnable() {

    @Override
    public void run() {

        i++;
        if(i==url.size())
            i=0;
        new Image(0,PhotoActivity.this).execute(url.get(i));
        handler.postDelayed(h, 2000);
    }
};




                  @Override
            public Context getContext() {
// TODO Auto-generated method stub
return null;
       } 


              @Override
                public String getMode() {
// TODO Auto-generated method stub
return null;
                   }


          @Override
             public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
        }


        @Override
             public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

            }


             @Override
           public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
            }


            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
    float distanceY) {
// TODO Auto-generated method stub
return false;
                  }


        @Override
             public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

                           }


              @Override
                 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
boolean result = false;
try {

    float diffX = e2.getX() - e1.getX();

    if (Math.abs(diffX) > SWIPE_THRESHOLD
            && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
        if (diffX > 0) {
            onSwipeRight();
        } else {
            onSwipeLeft();
        }
    }

} catch (Exception exception) {
    exception.printStackTrace();
}
return result;  
             }
               @Override
             public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(mode.equals(MainActivity.PHOTO))
    return swipe.onTouchEvent(event);
    else
return super.onTouchEvent(event);
                   }

            private void onSwipeLeft() {
i--;
if (url!= null) {

    if(i<0)
        i = url.size()-1;
    if (getDrawableFromMemCache(i) == null) {
    new Image(i,this).execute(url.get(i));
    }else
        setPhotos(getDrawableFromMemCache(i));
            }

        }


                 private void onSwipeRight() {
// TODO Auto-generated method stub
i++;
if (url != null) {
    if(i >url.size()-1)
        i=0;
    if (getDrawableFromMemCache(i) == null) {
            new Image(i,this).execute(url.get(i));
        }else
            setPhotos(getDrawableFromMemCache(i));
            }

            }


              private Bitmap getDrawableFromMemCache(int key) {
// TODO Auto-generated method stub

    return cache.get(key);

            }


             public LruCache<Integer, Bitmap> getCache() {
return cache;
                  }

           public void setCache(LruCache<Integer,Bitmap> cache) {
this.cache = cache;
                }

                   @Override
             public void addDrwabletoCache(Bitmap bm) {
// TODO Auto-generated method stub
if (getDrawableFromMemCache(i) == null) {
    cache.put(i, bm);
       }
   }
         @Override
         public void setPhotos(Bitmap bm) {
// TODO Auto-generated method stub
iv.setImageBitmap(bm);
       }

             }
HHK

可能getIntent().getStringArrayListExtra("msg");返回null导致返回urlnull。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

空指针运行时异常

空指针运行时异常erorr,但为什么呢?

空指针异常导致运行时错误

将来<T>获得()卡住时可运行的抛出异常 - 在单独执行服务运行时?

执行函数期间的运行时异常

当整个Maven项目在junit测试下运行时,logger.info引发空指针异常

在 Android Studio 中使用 ListView 时出现运行时错误(空指针异常)

尝试在Android中创建倒数计时器时出现运行时异常和空指针异常

运行时异常:执行doInBackground()时发生错误

在后台执行运行时异常

如何防止可运行的执行时,其中一抛出异常

为什么在编译时不检查空指针异常,例如在Java中迭代空集合而不是运行时异常时?

SDWebimage运行时异常

JavaFX运行时异常

SQL运行时异常

运行时gradle异常

奇怪的运行时异常

为什么我一运行我的应用程序就会在运行时收到空指针异常错误,它会崩溃?

运行时执行输出

皮棉改进后的可运行时崩溃

运行时三指针异常的规则(图形节点)

推荐的设计模式是什么,以避免在调用removeCallbacks()时在可运行对象中的引用上出现空指针异常

执行-运行时错误:无效的内存地址或nil指针取消引用

执行:紧急:运行时错误:无效的内存地址或nil指针取消引用

类型指针的运行时创建?

指针在运行时更改

如何修复运行时错误:加载'const char类型的空指针

运行时错误:“TreeNode”类型的空指针内的成员访问

运行时错误:引用绑定到“int”类型的空指针(合并函数)