每次嘗試運行此類時,應用程序都會關閉強制關閉。當我在Android設備或模擬器上運行此類時,它會告訴我應用程序已停止。在這個課程運行之前,它工作得很好。 Android Studio向我展示了沒有錯誤。我的Android應用程序在運行特定類時保持關閉狀態
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)
請創建一個最小的可覈查的例子 –
請發表您的崩潰logcat的! – iagreen
你似乎沒有初始化你的'ball'或'answerText'成員變量。 – iagreen