我正在开发一个Android应用程序。当我试图在模拟器上运行它时崩溃。这是我的第一个Android应用程序,我无法找出错误的原因。任何人都可以指出错误吗?
logcat的
Shutting down VM
FATAL EXCEPTION: main
Process: io.github.ishankgulati.breakout, PID: 3589
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{io.github.ishankgulati.breakout/io.github.ishankgulati.breakout.BreakoutGame}: java.lang.IllegalAccessException: void io.github.ishankgulati.breakout.BreakoutGame.<init>() is not accessible from java.lang.Class<android.app.Instrumentation>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalAccessException: void io.github.ishankgulati.breakout.BreakoutGame.<init>() is not accessible from java.lang.Class<android.app.Instrumentation>
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-29 19:10:34.689 3589-3589/? I/Process: Sending signal. PID: 3589 SIG: 9
您可以查看我的项目here。
上述问题已得到解决,但应用程序仍在崩溃。
logcat的
I/art: Not late-enabling -Xcheck:jni (already on)
I/art: Late-enabling JIT
I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
W/System: ClassLoader referenced unknown path: /data/app/io.github.ishankgulati.breakout-1/lib/x86
E/WVMExtractor: Failed to open libwvm.so: dlopen failed: library "libwvm.so" not found
I/OMXClient: Using client-side OMX mux.
D/MediaPlayer: setSubtitleAnchor in MediaPlayer
W/art: Suspending all threads took: 33.410ms
I/OMXClient: Using client-side OMX mux.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.ishankgulati.breakout, PID: 2303
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.github.ishankgulati.breakout/io.github.ishankgulati.breakout.BreakoutGame}: java.lang.NullPointerException: Attempt to write to field 'float android.graphics.RectF.left' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to write to field 'float android.graphics.RectF.left' on a null object reference
at io.github.ishankgulati.breakout.MainMenu.<init>(MainMenu.java:33)
at io.github.ishankgulati.breakout.BreakoutGame$BreakoutView.<init>(BreakoutGame.java:84)
at io.github.ishankgulati.breakout.BreakoutGame$BreakoutView.<init>(BreakoutGame.java:51)
at io.github.ishankgulati.breakout.BreakoutGame.onCreate(BreakoutGame.java:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:3)
您的BreakoutGame
public class BreakoutGame extends Activity{
正在覆盖链接其可见性的默认构造函数。这导致异常。不要这样做。每个初始化,你想在你的构造函数中做的事情都可以在框架调用的一个回调中完成。 E.g onCreate
答案 1 :(得分:3)
这是因为在您的BreakoutGame
活动中,您有一个没有任何access modifiers的空构造函数(默认为包私有)。
BreakoutGame() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenX = size.x;
screenY = size.y;
}
请将其更改为
public BreakoutGame(){
.....
}
一定会奏效。
再次将代码从构造函数移动到onCreate
方法。
答案 2 :(得分:0)
我收到此异常,是因为我在修复棉绒错误时从App.java中删除了公共访问修饰符。重新添加公共关键字即可立即对其进行修复。
public class MyApp extends Application