从一个班级到另一个班级的意图不起作用

时间:2015-05-10 03:52:45

标签: android android-intent

我在运行此应用程序时在title_page_activity.java中创建了一个intent,标题页出现,应用程序停止运行。 并且DDMS说应用程序在其主线程上做了更多的工作。 请帮帮我这个

清单

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

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Title_page_activity"
            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" >
            <intent-filter>
                <action android:name="com.app.hems.cqu.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

mainpage.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.hems.cqu.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@style/AppBaseTheme"
        android:textSize="80sp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="22dp"
        android:text="@string/contact"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:typeface="sans" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/signin"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="34dp"
        android:text="@string/signup"
        android:textStyle="bold" />

</RelativeLayout>

titlepage.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/title_image"
    >

</RelativeLayout>

MainActivity.java

package com.app.hems.cqu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle viewUserOptionPage) {
        super.onCreate(viewUserOptionPage);
        setContentView(R.layout.mainpage);
    }


}

Title_Page_Activity.java

package com.app.hems.cqu;

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

public class Title_page_activity extends Activity {

    @Override
    protected void onCreate(Bundle CreateTitleSleep) {
        // TODO Auto-generated method stub
        super.onCreate(CreateTitleSleep);
        setContentView(R.layout.titlepage);
        Thread Timer = new Thread(){
            public void run()
            {
                try
                {
                    sleep(2000);

                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    Intent openMainActivity = new Intent("com.app.hems.cqu.MAINACTIVITY");
                    startActivity(openMainActivity);
                }               
            }           
        };
        Timer.start();
        }

    }

3 个答案:

答案 0 :(得分:1)

在onCreate中你启动一个线程(好的,你想从主ui卸载),但在那个线程中你再次启动mainactivity。所以这将最终导致无限循环的活动开始。

答案 1 :(得分:1)

试试这个,onResume是创建活动的最后一种方法,同时检查activity lifecycle

@Override
protected void onResume() {
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        Intent openMainActivity = new Intent("com.app.hems.cqu.MAINACTIVITY");
        startActivity(openMainActivity);
      }
    }, 2000);
}

答案 2 :(得分:0)

您需要在intent中提供上下文。试试这个

finally
            {
                Intent openMainActivity = new Intent(this, MainActivity.class);
                startActivity(openMainActivity);
            }