如何解决当同一个类中有一个空的构造函数时,是否会绕过带有 args 的构造函数?
当 classtwo.classtwoMethod() 被调用时,classtwo 是如何为空的?我假设这与有一个空的 args 构造函数有关,但我不知道为什么。
public class ClassOne extends AccessibilityService {
IClasstwo classtwo;
public ClassOne(){}
public ClassOne(IClasstwo classtwo){
this.classtwo = classtwo;
}
@Override
public boolean onKeyEvent(KeyEvent event) {
classtwo.classtwoMethod();
return super.onKeyEvent(event);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
}
我在 MyApp 类中调用构造函数,在其中传递 Classtwo 的实例,如下所示:
public class MyApp extends Application {
Classtwo classtwo;
ClassOne classOne;
@Override
public void onCreate() {
super.onCreate();
classtwo = new Classtwo();
classOne = new ClassOne(classtwo);
checkAccessibilityPermission();
}
public boolean checkAccessibilityPermission() {
int accessEnabled=0;
try {
accessEnabled = Settings.Secure.getInt(this.getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
e.printstacktrace();
}
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return false;
}
}
但后来我得到了错误: 引起:java.lang.NullPointerException:尝试在 classtwo.classtwoMethod() 处的空对象引用上调用接口方法“void com.example.wearableinterfaceproject.IClasstwo.classtwoMethod()”;
这里是 Classtwo:
public class Classtwo implements IClasstwo {
@Override
public void classtwoMethod() {
//irrelevant
}
}
这是堆栈跟踪:
2021-04-04 18:42:33.609 32480-32480/com.example.wearableinterfaceproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wearableinterfaceproject,PID: 32480
java.lang.RuntimeException: Unable to create application com.example.wearableinterfaceproject.MyApp: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.wearableinterfaceproject.IClasstwo.classtwoMethod()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6717)
at android.app.ActivityThread.access$1300(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.wearableinterfaceproject.IClasstwo.classtwoMethod()' on a null object reference
at com.example.wearableinterfaceproject.ClassOne.method(ClassOne.java:19)
at com.example.wearableinterfaceproject.ClassOne.<init>(ClassOne.java:15)
at com.example.wearableinterfaceproject.MyApp.onCreate(MyApp.java:17)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6712)
at android.app.ActivityThread.access$1300(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
解决方法
您的 onKeyEvent 方法超出了 ClassOne 的范围,因此您无法直接访问您的 classTwo 变量。通过应用程序状态获取您的 classTwo 变量。在您的 onkeyevent 方法中添加以下代码,并将 getClassOne 方法添加到返回 ClassOne 实例的 MyApp 类中。
MyApp appState = ((MyApp)this.getApplication());
ClassOne co = appState.getClassOne();
co.classTwo.classTwoMethod();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。