我的应用程序将无法运行,也不会切换活动

时间:2011-12-26 22:40:56

标签: android loops android-intent android-activity manifest

我想创建一个启动画面,然后移动到登录/注册屏幕。我的代码如下所示:

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;

public class AssaultTDActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.TimeOut();
}

public void TimeOut(){
    long start = System.currentTimeMillis();
    boolean continueloop = true;
    long timenow;

    while (continueloop = true){
        timenow = System.currentTimeMillis();
        if (timenow - start > 5000){
            continueloop = false;
            this.GoToRegister();
        }
    }
}

public void GoToRegister(){
    Intent i = new Intent(AssaultTDActivity.this, register_activity.class);
    startActivity(i);
    finish();
}

}

import android.app.Activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;


public class register_activity extends Activity {
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.register);

        }
}

我的清单文件:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity android:screenOrientation="landscape"
        android:label="@string/app_name"
        android:name=".AssaultTDActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity class =".register_activity"  
        android:label="Log in" 
        android:screenOrientation="landscape" 
        android:name=".register_activity" >

    </activity>


</application>

我在这里做错了吗? 还有一个命令在循环时“执行事件”,这样你就会陷入循环中吗?

1 个答案:

答案 0 :(得分:1)

我建议您允许Android使用Handler及其 postDelayed 方法处理您的超时:

public class SplashPlayActivity extends Activity {
    private Handler mHandler;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                GoToRegister();
                finish();
            }
        }, 5000 /** timout in millis */);
    }

    private void GoToRegister() {
        // start the activity here
    }
}