我有一个Android项目,所有源代码都没有错误,但有时会给我一个力量关闭。
这是我的代码:
timecompletedialog.java
public class QuizActivity extends BaseFragmentActivity implements OnClickListener,AnswerListener,
OnTimeCompleteListener{
private TextView questionTextView,timer_text,score_text;
private ImageButton option_1, option_2, option_3, option_4;
private LinearLayout layout;
public static int LEVEL=0,timer=0;
private Handler handler=new Handler();
private OnTimeCompleteListener timeComplete=(OnTimeCompleteListener)this;
private Runnable timerThread=new Runnable() {
@Override
public void run() {
if(timer>0){
//Time is running
timer--;
timer_text.setText("Time : "+timer);
handler.postDelayed(this, 1000);
}else{
timeComplete.onTimeFinish();
}
}
};
private int QUESTION_COUNTER=1;
private int SCORE_COUNTER=0;
public static int QUESTION_LIMIT;
private CurrentQuestion currentQuestion ;
UtilityFile utility;
private static final String TEST_DEVICE_ID = "TES_EMULATOR";
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.quiz_activity_layout);
AdView adView = (AdView) this.findViewById(R.id.adView5);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(TEST_DEVICE_ID).build();
adView.loadAd(adRequest);
layout=(LinearLayout)this.findViewById(R.id.subject_animate1);
score_text=(TextView)this.findViewById(R.id.score);
timer_text=(TextView)this.findViewById(R.id.time);
score_text.setText("Score : "+"0/"+QUESTION_LIMIT);
questionTextView = (TextView) this.findViewById(R.id.quiz_question);
option_1 = (ImageButton) this.findViewById(R.id.quiz_level_option_1);
option_2 = (ImageButton) this.findViewById(R.id.quiz_level_option_2);
option_3 = (ImageButton) this.findViewById(R.id.quiz_level_option_3);
option_4 = (ImageButton) this.findViewById(R.id.quiz_level_option_4);
option_1.setOnClickListener(this);
option_2.setOnClickListener(this);
option_3.setOnClickListener(this);
option_4.setOnClickListener(this);
/**Utility for formating of the question*/
utility=new UtilityFile();
refreshQuestion();
handler.postDelayed(timerThread, 10);
Log.i("questionset", ""+QuizQuestionHandler.lev1.size());
}
public void refreshQuestion(){
List<Integer> randomOptionOrdering=new ArrayList<Integer>();
currentQuestion = utility
.setCurrentQuestionSet(QuizQuestionHandler.lev1);
ImageButton buttons[]={option_1,option_2,option_3,option_4};
int answerIndex=utility.randInt(0, 3);
Log.i("answertag",""+answerIndex);
//Right Answer
buttons[answerIndex].setImageDrawable(getResources().getDrawable(
currentQuestion.getCurrentSet().getAnsImage()));
buttons[answerIndex].setTag((Object)currentQuestion.getCurrentSet().getId());
questionTextView.setText(""
+ currentQuestion.getCurrentSet().getQuestion());
//Options
buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
currentQuestion.getOption_1().getAnsImage()));
buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
currentQuestion.getOption_2().getAnsImage()));
buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
currentQuestion.getOption_3().getAnsImage()));
}
public int randomOrder(List<Integer> rand,int currentAnswerIndex){
while(true){
int index = new UtilityFile().randInt(0,3);
if(index!=currentAnswerIndex){
if (!isInserted(rand, index)) {
rand.add(index);
Log.i("return",""+index);
return index;
}
}
}
}
public boolean isInserted(List<Integer> inserted,int currentIndex){
for(Integer inte:inserted){
if(inte==currentIndex){
return true;
}
}
return false;
}
@Override
public void onClick(View v) {
disableOptionButton();
AnswerFinder finder=null;
switch (v.getId()) {
case R.id.quiz_level_option_1:
finder=new AnswerFinder(option_1,currentQuestion , this);
finder.getAnswer();
break;
case R.id.quiz_level_option_2:
finder=new AnswerFinder(option_2,currentQuestion , this);
finder.getAnswer();
break;
case R.id.quiz_level_option_3:
finder=new AnswerFinder(option_3,currentQuestion , this);
finder.getAnswer();
break;
case R.id.quiz_level_option_4:
finder=new AnswerFinder(option_4,currentQuestion , this);
finder.getAnswer();
break;
default:
break;
}
}
private void disableOptionButton(){
option_1.setClickable(false);
option_2.setClickable(false);
option_3.setClickable(false);
option_4.setClickable(false);
}
private void enableOptionButton(){
option_1.setClickable(true);
option_2.setClickable(true);
option_3.setClickable(true);
option_4.setClickable(true);
}
public void animateNext(){
Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
layout.startAnimation(anim);
anim.setDuration(200);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
stopTimer();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
/**Handling Animation Delays and Render Other Animations
* */
Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.left_to_right);
anim.setDuration(200);
refreshQuestion();
layout.startAnimation(anim);
startTimer();
}
});
}
public void startTimer(){
handler.postDelayed(timerThread, 100);
}
public void stopTimer(){
handler.removeCallbacks(timerThread);
}
/**For getting the call of right and wrong answer*/
@Override
public void onAnswerClick(boolean answer) {
/**Check for the question overflow*/
if(QUESTION_COUNTER<QUESTION_LIMIT){
if(answer){
SCORE_COUNTER++;
AnswerHandler.rightAnswerHandler(score_text,this,SCORE_COUNTER,QUESTION_LIMIT);
}else{
//AnswerHandler.wrongAnswerHandler(this);
}
animateNext();
QUESTION_COUNTER++;
}else{
/**Incrementing the final score*/
SCORE_COUNTER++;
AnswerHandler.finalAnswerPlayer(this);
this.gameCompleted();
}
enableOptionButton();
}
public void gameCompleted(){
GameCompleteDialog dialog=new GameCompleteDialog(QuizActivity.this,SCORE_COUNTER);
dialog.buildDialog();
dialog.showDialog();
handler.removeCallbacks(timerThread);
}
public void onTimeFinish() {
TimeCompleteDialog dialog=new TimeCompleteDialog(this);
dialog.showDialog();
AnswerHandler.vibrate(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}
在此代码中没有错误。但我不知道出了什么问题。
这是我的问答活动
09-28 02:56:58.303: E/AndroidRuntime(2180): FATAL EXCEPTION: main
09-28 02:56:58.303: E/AndroidRuntime(2180): Process: com.sendquiz.guessanimalsquiz, PID: 2180
09-28 02:56:58.303: E/AndroidRuntime(2180): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b2282c30 is not valid; is your activity running?
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.app.Dialog.show(Dialog.java:286)
09-28 02:56:58.303: E/AndroidRuntime(2180): at com.sendquiz.javafile.TimeCompleteDialog.showDialog(TimeCompleteDialog.java:46)
09-28 02:56:58.303: E/AndroidRuntime(2180): at com.sendquiz.guessanimalsquiz.QuizActivity.onTimeFinish(QuizActivity.java:254)
09-28 02:56:58.303: E/AndroidRuntime(2180): at com.sendquiz.guessanimalsquiz.QuizActivity$1.run(QuizActivity.java:53)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.os.Handler.handleCallback(Handler.java:733)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.os.Looper.loop(Looper.java:136)
09-28 02:56:58.303: E/AndroidRuntime(2180): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-28 02:56:58.303: E/AndroidRuntime(2180): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 02:56:58.303: E/AndroidRuntime(2180): at java.lang.reflect.Method.invoke(Method.java:515)
09-28 02:56:58.303: E/AndroidRuntime(2180): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-28 02:56:58.303: E/AndroidRuntime(2180): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-28 02:56:58.303: E/AndroidRuntime(2180): at dalvik.system.NativeStart.main(Native Method)
这是我的logcat:
<?xml version="1.0"?>
<ArrayOfSpokenLanuageInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SpokenLanuageInfo>
<UserID>18</UserID>
<LanguageId>8</LanguageId>
<Language>Basque</Language>
</SpokenLanuageInfo>
<SpokenLanuageInfo>
<UserID>45</UserID>
<LanguageId>21</LanguageId>
<Language>Corsican</Language>
</SpokenLanuageInfo>
<SpokenLanuageInfo>
<LanguageId>27</LanguageId>
<Language>Esperanto</Language>
</SpokenLanuageInfo>
</ArrayOfSpokenLanuageInfo>
你可以告诉我哪里出错了吗?
答案 0 :(得分:3)
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@b2282c30无效;是你的活动 运行
当您在当前Activity上调用finish()后尝试显示对话框时,通常会发生这种情况。在你的代码中,这发生了两次。在TimeCompleteDialog
和onKeyDown
中。可能会发生的事情是你在Activity上调用finish(),而忘记清理Runnable的队列。调用timeComplete.onTimeFinish();
时,在这种情况下,您将遇到此类崩溃。如果我在你身边,我采取的第一个动作是在暂停活动时清理处理程序的队列。第二件事是在显示弹出窗口之前检查活动的isRunning
标志。
E.g。
if (!isFinishing()) {
// show popup
}
答案 1 :(得分:1)
在我导航到另一个活动后(当该活动在后台时),通过强制代码显示一个对话框,我能够在我的应用程序中重现这一点。
修复是在显示对话框之前添加以下检查:
if (activity == null) {
return false;
} else {
return !activity.isDestroyed() && !activity.isFinishing();
}
答案 2 :(得分:0)
出现此问题是,传递的上下文/活动不是活动的上下文/活动。 您应该通过当前活动或上下文