我很擅长开发并制作一个简单的“Whack-a-Mole”应用程序。我需要在整个程序中使用一些变量,例如报告痣是上升还是下降的变量。如果我在此之后定义变量:
public class WhackAMole extends Activity {
...我的应用程序崩溃并给我一条消息,说应用程序已停止。
如果我将变量放在onCreate
函数中,然后将它们传递给每个函数,我的处理程序会用红色加下划线并说The Constructor Object (my variables) is undefined.
- 即使我已经在任何地方定义了它。这是它的外观:
mHandler.postDelayed(new Runnable(Mole1_Btn, Mole2_Btn, Mole3_Btn, mole1_on, mole2_on, mole3_on)
{
public void run(ImageView Mole1_Btn, ImageView Mole2_Btn, ImageView Mole3_Btn, boolean mole1_on, boolean mole2_on, boolean mole3_on)
{
changeMole1(Mole1_Btn, Mole2_Btn, Mole3_Btn, mole1_on, mole2_on, mole3_on);
}
}, wait);
}
这一切都有下划线。为什么在第一个例子中崩溃,或者为什么处理程序给我第二个错误?
这是我将变量放在开头所说的位置时的错误日志:
08-08 12:05:27.622: D/libEGL(9830): loaded /system/lib/egl/libEGL_adreno200.so
08-08 12:05:27.622: D/libEGL(9830): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
08-08 12:05:27.632: D/libEGL(9830): loaded /system/lib/egl/libGLESv2_adreno200.so
08-08 12:05:27.632: I/Adreno200-EGL(9830): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3544079)
08-08 12:05:27.632: I/Adreno200-EGL(9830): Build Date: 03/28/13 Thu
08-08 12:05:27.632: I/Adreno200-EGL(9830): Local Branch: adreno_20130328
08-08 12:05:27.632: I/Adreno200-EGL(9830): Remote Branch:
08-08 12:05:27.632: I/Adreno200-EGL(9830): Local Patches:
08-08 12:05:27.632: I/Adreno200-EGL(9830): Reconstruct Branch:
08-08 12:05:27.702: D/OpenGLRenderer(9830): Enabling debug mode 0
08-08 12:06:06.750: D/GestureDetector(9830): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0
08-08 12:06:06.811: D/AndroidRuntime(9830): Shutting down VM
08-08 12:06:06.811: W/dalvikvm(9830): threadid=1: thread exiting with uncaught exception (group=0x41be1ac8)
08-08 12:06:06.821: E/AndroidRuntime(9830): FATAL EXCEPTION: main
08-08 12:06:06.821: E/AndroidRuntime(9830): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.WhackAMole}: java.lang.NullPointerException
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread.access$700(ActivityThread.java:151)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.os.Looper.loop(Looper.java:137)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread.main(ActivityThread.java:5293)
08-08 12:06:06.821: E/AndroidRuntime(9830): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 12:06:06.821: E/AndroidRuntime(9830): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 12:06:06.821: E/AndroidRuntime(9830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-08 12:06:06.821: E/AndroidRuntime(9830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-08 12:06:06.821: E/AndroidRuntime(9830): at dalvik.system.NativeStart.main(Native Method)
08-08 12:06:06.821: E/AndroidRuntime(9830): Caused by: java.lang.NullPointerException
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.Activity.findViewById(Activity.java:1886)
08-08 12:06:06.821: E/AndroidRuntime(9830): at com.example.myfirstapp.WhackAMole.<init>(WhackAMole.java:17)
08-08 12:06:06.821: E/AndroidRuntime(9830): at java.lang.Class.newInstanceImpl(Native Method)
08-08 12:06:06.821: E/AndroidRuntime(9830): at java.lang.Class.newInstance(Class.java:1319)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
08-08 12:06:06.821: E/AndroidRuntime(9830): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
08-08 12:06:06.821: E/AndroidRuntime(9830): ... 11 more
答案 0 :(得分:0)
这不是可行的方式。你需要不使用构造函数而不使用args运行()然后你需要在传递给这样的匿名类的任何变量上使用final关键字。
答案 1 :(得分:0)
这是一个错误的实现,正确的Runnable实现具有这个基本结构
new Runnable() {
@Override
public void run() {
//the code to be run
}
};
编辑:要在run()中使用参数,您可以将它们声明为最终