java - 使用线程的 Intent 不起作用

标签 java android xml android-intent android-activity

我知道我已经问过这个问题,但我没有得到足够的答案。我试图启动一个 Activity ,但模拟器停留在第一个 Activity 上。我已经尝试了所有方法,但从未奏效。 YouTube 视频显示它应该可以工作,但从来没有。下面的代码是否有遗漏或者有什么问题?

//First Activity:


package com.mtprogramming.magicsquaresgame;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;

public class Opening extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opening);
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                Intent open = new Intent("com.mtprogramming.magicsquaresgame.MENU");
                startActivity(open);
            }

        }
    };
    timer.start();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.opening, menu);
    return true;
}

}

//Second Activity:

package com.mtprogramming.magicsquaresgame;

import android.app.Activity;
import android.os.Bundle;


//Created by suprav on 7/11/13.

public class Menu extends Activity {
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
}
}

//Android Manifest:

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

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mtprogramming.magicsquaresgame.Opening"
        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="com.mtprogramming.magicsquaresgame.Menu"
        android:label="@string/title_activity_menu"
        android:parentActivityName="Opening" >
        <intent-filter>
            <action android:name="com.mtprogramming.magicsquaresgame.MENU"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="Opening" />
    </activity>
</application>

</manifest>

最佳答案

试试这个:

Intent open = new Intent(Opening.this, Menu.class);
startActivity(open);

关于java - 使用线程的 Intent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17605448/

相关文章:

xml - R devtools 失败为 "Package libxml-2.0 was not found in the pkg-config search path"

java - 我不知道哪些内容需要我的信标应用程序

java - Geotools 可以加载 DEM 吗?

java - 如何动态地将表(sqlite)中的行添加到布局? (安卓)

android - 将 startMethodTracing 和 stopMethodTracing 与 ddms 结合使用

android - 数据库异常 : Class is missing a constructor with no arguments

java - 如果表属性有默认值clock_timestamp(),如何注释实体中的属性?

java - 我们是否必须放置任何 .XML 标签才能包含或继承 GWT 中的 .properties 文件?

c# - 使用 LiNQ 加载嵌套的 XML

xml - eclipse xml 编辑器为空格插入制表符