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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15568234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:05:48  来源:igfitidea点击:

null pointer exception on calling interface method implemented in other class

javaandroidnullpointerexception

提问by My God

I am trying to call the method getFailureDialog()of the interface OnSelectedListener. The method is implemented in MainActivity.java. But when I call the method, I am getting the null pointer exception.

我正在尝试调用getFailureDialog()接口的方法OnSelectedListener。该方法在MainActivity.java. 但是当我调用该方法时,我收到了空指针异常。

I know that its because OnSelectedListeneris still not initialized and you are calling getFailureDialog()on uninitialized object. Obviously, interface methods are never initialized. But then how do I call the method getFailureDialog()from my class Common.java?

我知道这是因为OnSelectedListener尚未初始化并且您正在调用getFailureDialog()未初始化的对象。显然,接口方法永远不会被初始化。但是,我如何getFailureDialog()从我的班级中调用该方法Common.java

I am placing only the relevant source code below-

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

Source code:

源代码:

SharedFragment.java

共享片段.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

主活动.java

public class MainActivity implements Common.OnSelectedListener
{

@Override
    public void getFailureDialog()
    {

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

Error Log

错误日志

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)

回答by Kylar

OnSelectedListener mCallback;

is never getting initialized, or is being initialized with a null value.

永远不会被初始化,或者被初始化为空值。

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.
    }
}

回答by Droidman

hereis the source code for the OnSelectedListener. Since that is an interface, you have to initialize it using newand overriding the onSelected()method OR let your class implement this listener

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

回答by Naveen

add a method in

添加一个方法

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
    }
}

now use the setOnSelectedListener()to initialize your listener

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

but from you code you might need to implement another listener in your SharedFragmenttoo.

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

回答by Dion Dirza

You have to redesign your code man, make Common class implement OnSelectedListenerinterface. So separate OnSelectedListeneras outer interface not as inner interface.

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

i will code it like this.

我会这样编码。

public interface OnSelectedListener 
{
    public void getFailureDialog();
}

then Commonclass should be like this

那么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
    }
}

and this code will run smoothly. if you need to implement the getFailureDialog in MainActivity, make this Common class as abstract class.

并且此代码将顺利运行。如果您需要在 MainActivity 中实现 getFailureDialog,请将这个 Common 类设为抽象类。

Common class will be like this

普通班会是这样

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
    }
}

now your MainActivityclass can extend from this class to implement the missing part.

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

回答by MoleIsKing

You need to modify Common and MainActivity. In Common add a basic constructor. Then activate the callback as shown in startMyCallback.

需要修改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

主活动.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");
    }
}