gpt4 book ai didi

java - 无法从线程启动 Activity

转载 作者:行者123 更新时间:2023-11-29 08:06:11 25 4
gpt4 key购买 nike

我无法从线程开始 Activity 。我想在 2 秒后开始一项 Activity 。但它给出错误 - 应用程序意外停止。

这是我要从中运行线程的 Activity 的代码。

package com.pack.prog;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

public class Splash extends Activity {

MediaPlayer mPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.splash);

mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
mPlayer.start();

Thread thread = new Thread() {
public void run() {

try {
sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(Splash.this, e.toString(),
Toast.LENGTH_LONG).show();
} finally {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}

};
};
thread.start();

} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}

@Override
protected void onPause() {

try {
super.onPause();
if (mPlayer.isPlaying()) {
mPlayer.release();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}

@Override
public void onBackPressed() {
try {
super.onBackPressed();
if (mPlayer.isPlaying()) {
mPlayer.release();
}
} catch (Exception w) {
Toast.makeText(this, w.toString(), Toast.LENGTH_LONG).show();
} finally {
finish();
}
}

}

这是 list 文件。

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

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Splash"
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=".StartingMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pack.prog.STARTINGMENU" />

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


</application>

</manifest>

我认为只有 list 有问题。

最佳答案

我认为操作名称不同于 "com.pack.prog.StartingMenu" 和 list "com.pack.prog.STARTINGMENU"

并且还在 UI 线程中执行此操作 .....

  mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments);
  mPlayer.start();
new Handler().postDelayed(new Runnable() {

public void run() {

Intent i = new Intent("com.pack.prog.STARTINGMENU"); //<--- what ever which is right
startActivity(i);
}
}, 2000);

关于java - 无法从线程启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com