从一个 class 到另一个不工作的意图
Intent from one class to another in not working
我在 title_page_activity.java 中创建一个意图,当我 运行 这个应用程序出现标题页时,不幸的是应用程序停止了。
并且 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();
}
}
您需要提供意图内部的上下文。试试这个
finally
{
Intent openMainActivity = new Intent(this, MainActivity.class);
startActivity(openMainActivity);
}
在 onCreate 中您启动了一个线程(很好,您想从主线程中卸载 ui),但在该线程中您再次启动了主线程activity。所以这将以 activity 开始的无限循环结束。
试试这个,onResume
是创建 activity 的最后一个方法,还要检查 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);
}
我在 title_page_activity.java 中创建一个意图,当我 运行 这个应用程序出现标题页时,不幸的是应用程序停止了。 并且 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();
}
}
您需要提供意图内部的上下文。试试这个
finally
{
Intent openMainActivity = new Intent(this, MainActivity.class);
startActivity(openMainActivity);
}
在 onCreate 中您启动了一个线程(很好,您想从主线程中卸载 ui),但在该线程中您再次启动了主线程activity。所以这将以 activity 开始的无限循环结束。
试试这个,onResume
是创建 activity 的最后一个方法,还要检查 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);
}