每当用户点击他/她手机上的应用程序图标时,我想让启动画面生效3秒钟。以下是我的代码。我不知道问题是什么。我没有任何错误(没有红色波浪线)这些文件。
这是我的splash.java
文件:
package org.example.name.tamsky;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread myThread = new Thread() {
@Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
这是我的activity_splash.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_splash"
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="org.example.name.tamsky.splash">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash"/>
</RelativeLayout>
这是我的AndroidManifest.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.name.tamsky">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
我尝试运行模拟器时遇到的错误是:
Error running MainActivity:
The activity must be exported or contain an intent-filter
答案 0 :(得分:0)
在您的Manifest.xml中尝试更改此行:
<activity android:name=".MainActivity"></activity>
到:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 1 :(得分:0)
使用处理程序
尝试此操作import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity extends AppCompatActivity {
private int SPLASH_DISPLAY_LENGTH=500;
Tracker mTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
},SPLASH_DISPLAY_LENGTH);
}
}