在其他类中实现的调用接口方法上的空指针异常

时间:2013-03-22 10:36:21

标签: java android nullpointerexception

我正在尝试调用接口getFailureDialog()的方法OnSelectedListener。 该方法在MainActivity.java中实现。但是当我打电话给这个方法时,我得到了 空指针异常。

我知道它是因为OnSelectedListener仍未初始化,而您在未初始化的对象上调用getFailureDialog()。显然,接口方法永远不会被初始化。但是,如何从我的班级getFailureDialog()调用方法Common.java

我只在下面放置相关的源代码 -

源代码:

SharedFragment.java

public class SharedFragment extends DialogFragment 
{       
    Bundle bundle = getArguments();
    final String email = bundle.getString("email");

    Thread t=new Thread(new Runnable() 
    {
        public void run() {
        common.myRecord(email);
    }
    });   t.start(); 
}

Common.java

public class Common
{
OnSelectedListener mCallback;


    public interface OnSelectedListener 
    {
        public void getFailureDialog();
    }

    public void myRecord(String email)
    {
        mCallback.getFailureDialog();  //null pointer exception here
    }
}

MainActivity.java

public class MainActivity implements Common.OnSelectedListener
{

@Override
    public void getFailureDialog()
    {

        RecordFailure fd = new RecordFailure(); 
        fd.show(getSupportFragmentManager(), "dialog");
    }
}

错误记录

03-22 15:50:39.032: W/dalvikvm(20796): threadid=16: thread exiting with uncaught exception (group=0x4204c450)
03-22 15:50:39.052: E/AndroidRuntime(20796): FATAL EXCEPTION: Thread-30126
03-22 15:50:39.052: E/AndroidRuntime(20796): java.lang.NullPointerException
03-22 15:50:39.052: E/AndroidRuntime(20796):    at com.cornmail.util.Common.myRecord(Common.java:2062)

5 个答案:

答案 0 :(得分:7)

OnSelectedListener mCallback;

永远不会被初始化,或者正在使用空值进行初始化。

public class Common
{
    OnSelectedListener mCallback = new OnSelectedListener(){
        public void getFailureDialog(){
            JOptionPane.showMessageDialog(null, "An Error Has Occurred.");
        }
    };


    public interface OnSelectedListener 
    {
        public void getFailureDialog();
    }

    public void myRecord(String email)
    {
        mCallback.getFailureDialog();  //now this works.
    }
}

答案 1 :(得分:0)

hereOnSelectedListener的源代码。由于这是一个接口,您必须使用 new 初始化它并覆盖onSelected()方法或让您的类实现此侦听器

答案 2 :(得分:0)

中添加方法
public class Common
{
    OnSelectedListener mCallback;

    public void setOnSelectedListener(OnSelectedListener listener){
        mCallback = listener;
    }

    public interface OnSelectedListener 
    {
        public void getFailureDialog();
    }

    public void myRecord(String email)
    {
        mCallback.getFailureDialog();  //null pointer exception here
    }
}

现在使用setOnSelectedListener()初始化您的侦听器

但是从您的代码中,您可能还需要在SharedFragment中实现另一个侦听器。

答案 3 :(得分:0)

您必须重新设计您的代码人,使Common类实现OnSelectedListener接口。因此,将OnSelectedListener与外部接口分开,而不是作为内部接口。

我会像这样编码。

public interface OnSelectedListener 
{
    public void getFailureDialog();
}

然后Common类应该是这样的

public class Common implements OnSelectedListener
{
    public void getFailureDialog()
    {
        RecordFailure fd = new RecordFailure(); 
        fd.show(getSupportFragmentManager(), "dialog");
    }

    public void myRecord(String email)
    {
        getFailureDialog();
        //do something more rather than just call existing method
    }
}

此代码将顺利运行。 如果需要在MainActivity中实现getFailureDialog,请将此Common类作为抽象类。

普通类将是这样的

public abstract class Common implements OnSelectedListener
{
    public abstract void getFailureDialog();

    public void myRecord(String email)
    {
        getFailureDialog();
        //do something more rather than just call existing method
    }
}

现在你的MainActivity类可以从这个类扩展来实现缺少的部分。

答案 4 :(得分:0)

您需要修改Common和MainActivity。在Common中添加一个基本构造函数。然后激活回调,如startMyCallback所示。

Common.java

public class Common
{

 public Common() {}

 OnSelectedListener mCallback;


 public interface OnSelectedListener 
 {
    public void getFailureDialog();
 }

 public void myRecord(String email)
 {
    mCallback.getFailureDialog();  //null pointer exception here
 }
}

MainActivity.java

public class MainActivity implements Common.OnSelectedListener
{

    Common common = new Common();

    public MainActivity()
    {

    }

    public void startMyCallback()
    {
        common.mCallback = this;
    }

    @Override
    public void getFailureDialog()
    {
        RecordFailure fd = new RecordFailure(); 
        fd.show(getSupportFragmentManager(), "dialog");
    }
}