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