计时器在Android Eclipse中不起作用

时间:2014-07-12 07:04:29

标签: android eclipse timer

计时器无效。几秒后,它应该转到另一个活动

public class intro extends Activity {
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.intro);
    final Toast tosta = Toast.makeText(this, String.valueOf(count+"."), Toast.LENGTH_SHORT);
    final Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                count++;

                if(count == 5){
                    Intent app = new Intent("com.jasrajcomputers.MainActivity");
                    startActivity(app);
                }
            tosta.show();
            }
        }, 1000, 1000);


}}

但不幸的是5秒后应用程序停止了。

manifext.xml

  <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jasrajcomputers"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/noactionbarvirat"
    android:label="@string/app_name" >
    <activity
        android:name=".intro"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jasrajcomputers.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.jasrajcomputers.MainActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

  </manifest>

我的LOGCAT:

07-12 12:46:38.979: E/AndroidRuntime(14495): FATAL EXCEPTION: Timer-0
07-12 12:46:38.979: E/AndroidRuntime(14495): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.jasrajcomputers.MainActivity }
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1697)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1492)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Activity.startActivityForResult(Activity.java:3388)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Activity.startActivityForResult(Activity.java:3349)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Activity.startActivity(Activity.java:3584)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at android.app.Activity.startActivity(Activity.java:3552)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at com.jasrajcomputers.intro$1.run(intro.java:72)
07-12 12:46:38.979: E/AndroidRuntime(14495):    at java.util.Timer$TimerImpl.run(Timer.java:284)

3 个答案:

答案 0 :(得分:0)

使用Handler

new Handler().postDelayed(new Runnable(){
@Override
public void run() {
     Intent app = new Intent("com.example.MainActivity");
     startActivity(app);
    }
}, 5000);

答案 1 :(得分:0)

尝试替换此代码:

Intent app = new Intent("com.jasrajcomputers.MainActivity");

使用此代码:

Intent app = new Intent(intro.this,MainActivity.class);

注意:

MainActivity must be define in AndroidManifest.xml

答案 2 :(得分:0)

只是

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/noactionbarvirat"
android:label="@string/app_name" >
<activity
    android:name=".intro"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
 <activity
    android:name=".MainActivity"
    android:label="@string/app_name" >

</activity>
</application>

使用显式意图

Intent app = new Intent(intro.this, MainActivity.class);

删除

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jasrajcomputers.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.jasrajcomputers.MainActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

更新:

这里不需要定时器。您可以改为使用Handler

引用文档

  

显式意图指定组件以名称开头(   完全合格的班级名称)。您通常会使用明确的意图   在您自己的应用中启动组件,因为您知道类名   您想要开始的活动或服务。例如,开始一个新的   响应用户操作或启动服务下载的活动   文件在后台。