Android 空指针异常怎么解决?

空指针异常

refreshableview.setOnRefreshListener(new PullToRefreshListener(){

        @Override
        public void onRefresh() {
            // TODO Auto-generated method stub
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            refreshableview.finishRefreshing();
        }

    }, 0);
RefreshableView中
     public void setOnRefreshListener(PullToRefreshListener listener, int id) {  
    mListener = listener;  
    mId = id;  
} 
    谢谢!

笔者在开发安卓的过程中经常遇到空指针异常的情况:java.lang.NullPointerException
举个例子:

public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.button1:
if (!isBind......
答案就在这里:Android中关于空指针异常的解决办法
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

是不是你的View还没有初始化,

只要明白了,什么是空指针异常,就知道怎么解决了?

在java中,更确切的说,应该是空引用异常,也就是一个对象,你没有给他分配内存,就开始使用这个对象,比如调用这个对象的方法,这时候,就会报空指针异常了。

怎么解决呢?很简单,不是没有分配内存吗,给他分配内存就行了。
怎么分配内存呢?也简单,初始化这个对象,这个对象分配了内存了。
如何初始化?你懂的...

知其所以然:NullPointerException 官方说明文档

/**

  • Thrown when an application attempts to use null in a
  • case where an object is required. These include:
    • Calling the instance method of a null object.
    • Accessing or modifying the field of a null object.
    • Taking the length of null as if it were an array.
    • Accessing or modifying the slots of null as if it
    • were an array.
    • Throwing null as if it were a Throwable
    • value.

    Applications should throw instances of this class to indicate other illegal uses of the null object. * @author unascribed @version 1.20, 11/17/05 @since JDK1.0 */