我的android应用程序在运行特定类时保持强制关闭

时间:2015-09-05 03:59:03

标签: java android

每次尝试运行此类时,应用程序都会强制关闭。当我在Android设备或模拟器上运行此类时,它会告诉我应用程序已停止。在这个类运行之前它运行得很好。 Android Studio没有显示任何错误。

import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class Predictions {

    private static Predictions predictions;
    private String[] answers;
    public ImageView ball;
    public TextView answerText;
    public Animation animationslideintop;

    private Predictions() {
       int randomNumber = (int) ((Math.random() * 4) + 1);
      if (randomNumber == 1) {
        answerText.setText("");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball02);
            }
        }, 100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball03);
            }
        }, 200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball04);
            }
        }, 300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball05);
            }
        }, 400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball06);
            }
        }, 500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball07);
            }
        }, 600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball08);
            }
        }, 700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball09);
            }
        }, 800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball10);
            }
        }, 900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball11);
            }
        }, 1000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball12);
            }
        }, 1100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball13);
            }
        }, 1200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball14);
            }
        }, 1300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball15);
            }
        }, 1400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball16);
            }
        }, 1500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball17);
            }
        }, 1600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball18);
            }
        }, 1700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball19);
            }
        }, 1800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball20);
            }
        }, 1900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball21);
            }
        }, 2000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball22);
            }
        }, 2100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball23);
            }
        }, 2200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball24);
            }
        }, 2300);
        answers = new String[] {
                "Your dreams will come true."
        };
        answerText.startAnimation(animationslideintop);
       }
      else if (randomNumber == 2) {
        answerText.setText("");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball02);
            }
        }, 100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball03);
            }
        }, 200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball04);
            }
        }, 300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball05);
            }
        }, 400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball06);
            }
        }, 500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball07);
            }
        }, 600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball08);
            }
        }, 700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball09);
            }
        }, 800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball10);
            }
        }, 900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball11);
            }
        }, 1000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball12);
            }
        }, 1100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball13);
            }
        }, 1200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball14);
            }
        }, 1300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball15);
            }
        }, 1400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball16);
            }
        }, 1500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball17);
            }
        }, 1600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball18);
            }
        }, 1700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball19);
            }
        }, 1800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball20);
            }
        }, 1900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball21);
            }
        }, 2000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball22);
            }
        }, 2100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball23);
            }
        }, 2200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball24);
            }
        }, 2300);
        answers = new String[] {
          "Your dreams will NEVER come true."
        };
        answerText.startAnimation(animationslideintop);
       }
       else if(randomNumber == 3) {
        answerText.setText("");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball02);
            }
        }, 100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball03);
            }
        }, 200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball04);
            }
        }, 300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball05);
            }
        }, 400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball06);
            }
        }, 500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball07);
            }
        }, 600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball08);
            }
        }, 700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball09);
            }
        }, 800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball10);
            }
        }, 900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball11);
            }
        }, 1000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball12);
            }
        }, 1100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball13);
            }
        }, 1200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball14);
            }
        }, 1300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball15);
            }
        }, 1400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball16);
            }
        }, 1500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball17);
            }
        }, 1600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball18);
            }
        }, 1700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball19);
            }
        }, 1800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball20);
            }
        }, 1900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball21);
            }
        }, 2000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball22);
            }
        }, 2100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball23);
            }
        }, 2200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball24);
            }
        }, 2300);
        answers = new String[] {
          "Your future does not exist."
        };
        answerText.startAnimation(animationslideintop);
       }
    else if(randomNumber == 4) {
        answerText.setText("");
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball02);
            }
        }, 100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball03);
            }
        }, 200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball04);
            }
        }, 300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball05);
            }
        }, 400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball06);
            }
        }, 500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball07);
            }
        }, 600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball08);
            }
        }, 700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball09);
            }
        }, 800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball10);
            }
        }, 900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball11);
            }
        }, 1000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball12);
            }
        }, 1100);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball13);
            }
        }, 1200);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball14);
            }
        }, 1300);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball15);
            }
        }, 1400);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball16);
            }
        }, 1500);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball17);
            }
        }, 1600);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball18);
            }
        }, 1700);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball19);
            }
        }, 1800);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball20);
            }
        }, 1900);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball21);
            }
        }, 2000);
        handler.postDelayed(new Runnable() {
            public void run() {
                ball.setImageResource(R.drawable.ball22);
            }
        }, 2100);
           handler.postDelayed(new Runnable() {
               public void run() {
                    ball.setImageResource(R.drawable.ball23);
              }
             }, 2200);
              handler.postDelayed(new Runnable() {
                 public void run() {
                     ball.setImageResource(R.drawable.ball24);
               }
             }, 2300);
             answers = new String[] {
               "You will be slaughtered. Brutally."
             };
             answerText.startAnimation(animationslideintop);
         }
     }


   public static Predictions get()

   {   
       if (predictions == null) {
           predictions = new Predictions();
       }
       return predictions;
  }

   public String getPrediction() {
    return answers[0];
 }

}

以下是错误日志:

Logcat: 09-05 00:04:28.173  24524-24524/<MyPackage> I/art﹕ Late-enabling
-Xcheck:jni 09-05 00:04:28.543  24524-24544/<MyPackage> D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true 09-05 00:04:28.572  24524-24524/<MyPackage> D/﹕ HostConnection::get() New Host Connection established 0xb3ecc750, tid 24524 09-05 00:04:28.596  24524-24524/<MyPackage> D/Atlas﹕ Validating map... 09-05 00:04:28.611  24524-24524/<MyPackage> E/SensorManager﹕ Exception dispatching input event. 09-05 00:04:28.611  24524-24524/<MyPackage> D/AndroidRuntime﹕ Shutting down VM 09-05 00:04:28.613  24524-24524/<MyPackage> E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: <MyPackage>, PID: 24524
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at <MyPackage>.Predictions.<init>(Predictions.java:20)
            at <MyPackage>.Predictions.get(Predictions.java:518)
            at <MyPackage>.CrystalBall$1.onSensorChanged(CrystalBall.java:56)
            at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:405)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:143)
            at android.os.Looper.loop(Looper.java:122)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

2 个答案:

答案 0 :(得分:0)

首先,您应该获得活动的context,然后尝试使用findViewById方法查找您所指的布局的XML ID。

因此,例如,您可以将代码修改为

private Predictions(Context context) {
    ball = (ImageView) ((Activity) context).findViewById(R.id.ball_image_view);
    answerText = (TextView) ((Activity) context).findViewById(R.id.answer_text_view);

    // The rest of the code here ...
}

public static Predictions get(Context context) {   
    if (predictions == null) {
        predictions = new Predictions(context);
    }
    return predictions;
}

但是表现明智,这仍然不够好。通常,您只应拨打findViewById一次。因此,更好的方法是尝试在主活动中找到这些元素,然后将其传递到Prediction类。

答案 1 :(得分:0)

检查你的android清单文件,根据你的问题你的android清单文件有问题