2

我正在尝试调用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(); 
}

通用.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)
4

5 回答 5

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.
    }
}
于 2013-03-22T10:37:21.940 回答
0

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

于 2013-03-22T10:52:41.043 回答
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

于 2013-03-22T10:55:33.120 回答
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类可以从这个类扩展来实现缺失的部分。

于 2013-03-22T11:03:48.087 回答
0

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

通用.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");
    }
}
于 2016-10-11T20:02:35.960 回答