我是android开发的初学者。我一直在尝试使用音频文件来处理这个启动画面,该文件在3秒后自行消失。那我的Mainactivity应该开始了。但是我的代码显示了一些我无法弄清楚的错误.java类的代码如下:`
public class splash extends Activity {
MediaPlayer mySound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mySound=MediaPlayer.create(splash.this, R.raw.crowd_sound);
mySound.start();
}
Thread kill_Thread= new Thread(){
public void run(){
try {
sleep(3000);
Intent splash_intent=new Intent(splash.this,MainLFC.class);
startActivity(splash_intent);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
kill_Thread.start();
@Override
protected void onPause() {
super.onPause();
mySound.release();
}
}
清单文件如下:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".MainLFC"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.inten**strong text**t.action.MAINLFC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:1)
要获得您假装必须在onCreate方法中调用线程的行为。为此你有两个选择,要么继续使用你的线程并在调用它的开始之前在onCreate中定义它,要么只使用一个Handler。 Bellow theres你的onCreate方法两种选择。
使用线程。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mySound=MediaPlayer.create(splash.this, R.raw.crowd_sound);
mySound.start();
Thread kill_Thread= new Thread(){
public void run(){
try {
sleep(3000);
Intent splash_intent=new Intent(splash.this,MainLFC.class);
startActivity(splash_intent);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
kill_Thread.start();
}
@Override
protected void onPause() {
super.onPause();
mySound.release();
}
使用处理程序
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mySound=MediaPlayer.create(splash.this, R.raw.crowd_sound);
mySound.start();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent splash_intent=new Intent(splash.this,MainLFC.class);
startActivity(splash_intent);
finish()
}
}, 3000);
}
@Override
protected void onPause() {
super.onPause();
mySound.release();
}
答案 1 :(得分:0)
应该在主(UI)线程中开始新活动。
如果您想通过主线程传达线程,可以使用library(data.table)
dcast(setDT(d), cut(TimeStamp, "5 mins") ~ Status, length)
# TimeStamp get send
# 1: 2016-04-02 07:02:00 5 1
并开始新的活动。
示例处理程序:
Handler
将msg发送到处理程序的示例:
Handler hander = new Handler(){
public void handleMessage(Message m){
Intent splash_intent=new Intent(splash.this,MainLFC.class);
startActivity(splash_intent);
}
};
答案 2 :(得分:0)
在创建方法之外尝试:
public class kill_Thread extends Thread{
public void run(){
try {
sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent splash_intent=new Intent(splash.this,MainLFC.class);
startActivity(splash_intent);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
finish();
}
}
};
您声明公共变量集:
public kill_Thread kt;
并在你的OnCreate电话中:
kt.start();