0

EDIT: I did what Tritan Man said and it obviously worked. Thanks.

I am very new to developing and am making a simple "Whack-a-Mole" app. I need a few variables throughout the whole program, such as one to report if the mole is up or down. If I define the variables right after this:

public class WhackAMole extends Activity {

...my app crashes and gives me a message saying the app has stopped.

If I put the variables in the onCreate function, then pass them on to each function, a handler of mine gets underlined in red and says The Constructor Object (my variables) is undefined. - even though I have defined it everywhere. This is how it looks:

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

This all becomes underlined. Why does this crash in the first example, or why does the handler gives me an error for the second one?

Here is the error log from when I put the variables where I said at the start:

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
4

2 回答 2

0

这是一个错误的实现,正确的 Runnable 实现具有这个基本结构

new Runnable() {
  @Override
  public void run() {
    //the code to be run
  }
};

编辑:要在 run() 中使用参数,您可以将它们声明为 final

于 2013-08-08T19:08:50.730 回答
0

这不是 runnable 的工作方式。您需要不使用构造函数和参数来运行(),然后您需要在传递给这样的匿名类的任何变量上使用 final 关键字。

于 2013-08-08T19:02:22.017 回答