简体   繁体   English

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

[英]My android application keeps force closing when running a specific class

App keeps force closing every time I try to run this class. 每当我尝试运行此类时,App都会保持关闭状态。 When I run this class on an android device or an emulator, it will tell me that the app has stopped. 当我在Android设备或模拟器上运行此类时,它将告诉我该应用程序已停止。 It works perfectly fine before this class is run. 在运行此类之前,它可以很好地工作。 Android Studio is showing me no errors. 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];
 }

}

Here is the error log: 这是错误日志:

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)

First of all, you should get the context of your activity and then try to find the XML id of the layout you're referring to using findViewById method. 首先,您应该获取活动的context ,然后尝试使用findViewById方法查找所引用布局的XML ID。

So, for example, you could modify your code into something like 因此,例如,您可以将代码修改为类似

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

But performance wise, this is still not good enough. 但是从性能角度来看,这仍然不够好。 Generally, you should only call findViewById once. 通常,您只应该调用一次findViewById Therefore, a better approach would be trying to find these elements in your main activity, then pass it into the Prediction class. 因此,一种更好的方法是尝试在您的主要活动中找到这些元素,然后将其传递给Prediction类。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM