简体   繁体   English

我的应用停止响应

[英]My application stops responding

So whenever I launch my app on my devices it runs fine but when I push a button it does nothing excepted stay highlited and then pops up saying its not responding here is the code. 因此,每当我在我的设备上启动我的应用程序它运行正常,但当我按下按钮它没有做任何例外保持高亮,然后弹出说它没有响应这里是代码。 I think its the way I have the code in the OnClickListerner. 我认为它是我在OnClickListerner中拥有代码的方式。 package com.dicamillo.alarm; 包com.dicamillo.alarm;

import java.util.Calendar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.TimePicker;

public class AlarmlockActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    TimePicker tp;
    Button set;
    int hour;
    int minuet;
    DigitalClock dc;
    Calendar calendar = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tp = (TimePicker) findViewById(R.id.tpAlarmTime);
    set = (Button) findViewById(R.id.bSet);
    dc = (DigitalClock) findViewById(R.id.digitalClock1);
    hour = calendar.get(Calendar.HOUR);
    minuet = calendar.get(Calendar.MINUTE);
    set.setOnClickListener(this);

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bSet:
        while (tp.getCurrentHour().intValue() != hour
                && tp.getCurrentMinute().intValue() != minuet) {
            tp.getCurrentHour().intValue();
            tp.getCurrentMinute().intValue();
            if (tp.getCurrentHour().intValue() == hour
                    && tp.getCurrentMinute().intValue() == minuet) {
                MediaPlayer mp = MediaPlayer.create(AlarmlockActivity.this,
                        R.raw.alarm);
            } 
        }
        break;
    }
}

} }

Anything in the onClick method runs on the UI thread. onClick方法中的任何内容都在UI线程上运行。 Your while loop is hanging the app and the OS is (rightfully) forcing it to stop. 你的while循环挂起应用程序,操作系统(正当)强制它停止。

Look into using an AsyncTask to get your code off the UI thread. 查看使用AsyncTask从UI线程中获取代码。 You should also pause after each loop iteration to avoid unnecessary CPU usage. 您还应该在每次循环迭代后暂停,以避免不必要的CPU使用。

Your while will basically loop forever on the main UI thread until the hour/minute from your TimePicker matches the current time. 你的while将基本上永远循环在主UI线程上,直到你的TimePicker的小时/分钟与当前时间匹配。 That's what's causing the ANR message from the OS. 这就是导致操作系统发出ANR消息的原因。

You shouldn't be running long operations on the UI thread. 您不应该在UI线程上运行长操作。 Either use Threads or Handlers or AsyncTasks to achieve the result you want. 使用Threads或Handler或AsyncTasks来实现您想要的结果。

你试过检查logcat吗?!

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

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