我无法从线程开始活动。 我想在2秒后开始一项活动。但它给出了错误 - 应用程序意外停止。
以下是我想要运行该线程的活动的代码。
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();
}
}
}
这是清单文件。
<?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>
我认为只有清单存在一些问题。
答案 0 :(得分:2)
我认为操作名称不同"com.pack.prog.StartingMenu"
和清单"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);
答案 1 :(得分:2)
试试这个
mediaplayer = MediaPlayer.create(getBaseContext(), R.raw.happy_moments);
mediaplayer.start();
MyThread myThread=new MyThread();
myThread.start();
}
class MyThread extends Thread
{
public void run(){
try{
Thread.sleep(2000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
mHandler.sendEmptyMessage(0);
}
}
Handler mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if ( )
{
MyThread myThread=new MyThread();
myThread.start();
}
else
{
Intent i=new Intent(firstactivity.this,secondactivity.class);
startActivity(i);
finish();
}
}
};
}
答案 2 :(得分:1)
首先您的操作不匹配Intent i = new Intent("com.pack.prog.StartingMenu");
在清单中它是<action android:name="com.pack.prog.STARTINGMENU" />
<强>第二强> 严重问题
您可以在线程中触摸UI部分,您可以通过将它们封装在runOnUiThread
下来与线程内的UI进行交互。
所以将代码更改为此。
Thread thread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Splash.this, e.toString(),
Toast.LENGTH_LONG).show();
}
});
} finally {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
});
}
};
};
thread.start();
答案 3 :(得分:0)
应该使用
runOnUiThread(new Runnable() {
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);
}
}
});
答案 4 :(得分:0)
你无法从工作线程中执行与Ui相关的东西
答案 5 :(得分:0)
我更喜欢使用Timer
执行基于时间的任务。检查以下实现:
Timer t =new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
Intent i = new Intent("com.pack.prog.StartingMenu");
startActivity(i);
}
}, 2000);