Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的计时器不工作_Android_Timer - Fatal编程技术网

Android中的计时器不工作

Android中的计时器不工作,android,timer,Android,Timer,我正在尝试让一个警报框在我的应用程序在Android上启动5秒后显示出来。但它不起作用,我不知道为什么。代码如下: package com.example.helloandroid; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; imp

我正在尝试让一个警报框在我的应用程序在Android上启动5秒后显示出来。但它不起作用,我不知道为什么。代码如下:

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;



public class HelloAndroid extends Activity {

public void showalert() {
    /* alert box--------------------------- */
    AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
    dialog.setTitle("Test");
    dialog.setMessage("Test");
    dialog.setButton("Test 1",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton2("Test 2",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.setButton3("Test 3",
    new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton)
    {
    /* Do some stuff */
    }
    });
    dialog.show();
    /* -------------------------------------- */
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);


    new Timer().schedule(new TimerTask()
    { 
        public void run() 
        { 
            showalert();
        } 
    }, 5000);

    setContentView(R.layout.main);

}

}
有人能看出我做错了什么吗?为什么警报没有出现。。。?谢谢你的帮助

最好的威士忌, 迈克尔

编辑(这是经过一些修改后的工作代码):

package com.example.helloandroid;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;



public class HelloAndroid extends Activity {

    public void showalert() {
        /* alert box--------------------------- */
        AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
        dialog.setTitle("Test");
        dialog.setMessage("Test");
        dialog.setButton("Test 1",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton2("Test 2",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.setButton3("Test 3",
        new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int whichButton)
        {
        /* Do some stuff */
        }
        });
        dialog.show();
        /* -------------------------------------- */
    }

    public final void timerAlert(){ 

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                showalert();
            }
        }, 5000);

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timerAlert();
    }
}
尝试以下操作(保留showalert功能):

首先,像上面那样定义函数,然后在onCreate中放入
timerAlert()

尝试以下操作(保留showalert功能):

首先,像上面那样定义函数,然后在onCreate中放入
timerAlert()


也可以在处理程序中使用postdayed()方法

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        showalert();
    }
}, 5000);

您还可以在处理程序中使用postdayed()方法

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        showalert();
    }
}, 5000);
第一条路

new Timer().schedule(new TimerTask()
{
@Override 
   public void run()
   {
   // your action hear
   }
}, timeToExecute);
这将在
timeToExecute
秒后执行您的操作。它就像一个符咒,别忘了
@覆盖

使用
handle.postDelayed
第一种方式在编辑中发布时的第二种方式

new Timer().schedule(new TimerTask()
{
@Override 
   public void run()
   {
   // your action hear
   }
}, timeToExecute);
这将在
timeToExecute
秒后执行您的操作。它就像一个符咒,别忘了
@覆盖


第二种方式是使用
handle.postDelayed

在编辑中发布。你好,朱利安,谢谢你的回答!但我仍然无法使警报框出现。我必须对您的代码示例进行一次更改。我改变了这个:t=newtimer();tt=new TimerTask(){到这个:final Timer t=new Timer();TimerTask tt=new TimerTask(),然后我试着运行这个应用程序,但是没有警报框…你好,朱利安,谢谢你的回答!但是我仍然不能让警报框出现。我必须对你的代码示例做一个更改。我更改了这个:t=new Timer();tt=new TimerTask(){到这个:final Timer t=new Timer();TimerTask tt=new TimerTask()然后我尝试运行应用程序,但没有警报框…您好,GBouerat,谢谢您的回复!我尝试了您的建议,但无法使其正常工作。我必须删除您示例中的@Override,因为Eclipse抱怨运行()功能不同。好吧,现在我把你和朱利安的代码示例混合在一起,并让它工作起来:D非常感谢!我将尝试编辑我以前的帖子,以便你可以看到完整的工作代码,如果其他人也有同样的问题。你好,GBouerat,谢谢你的回复!我尝试了你的建议,但我无法让它工作。我不得不rem我不喜欢你的例子中的@Override,因为Eclipse在其他方面抱怨run()函数。现在我把你和Julian的代码示例混合在一起,并让它工作起来:D非常感谢!我将尝试编辑我以前的文章,这样你就可以看到完整的工作代码,如果其他人遇到同样的问题。