我正在尝试使用android studio创建启动画面。但是我在启动时遇到错误。它说不幸的是app_name已经停止了。
splashscreen.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="@color/colorPrimary" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/logo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="12dp"
android:textColor="#454545"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="OK Google" />
</RelativeLayout>
SplashScreen.java
public class SplashScreen extends Activity
{
// Splash screen timer
private static int SPLASH_TIME_OUT = 8000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sloid.splashgun">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.example.sloid.splashgun.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.sloid.splashgun.MainActivity"
android:label="@string/app_name"/>
</application>
</manifest>
我想创建一个启动画面并运行8秒。我想显示我的徽标和一些文字
错误说不幸app_name已停止。它关闭。
请帮忙。
MainActivity.java
package com.example.sloid.splashgun;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
LogCat报告
12-04 06:54:50.578 3399-3399/? I/art: Late-enabling -Xcheck:jni
12-04 06:54:50.581 3399-3399/? W/art: Unexpected CPU variant for X86 using defaults: x86
12-04 06:54:50.610 3399-3405/? E/art: Failed sending reply to debugger: Broken pipe
12-04 06:54:50.610 3399-3405/? I/art: Debugger is no longer active
12-04 06:54:50.610 3399-3405/? I/art: Starting a blocking GC Instrumentation
12-04 06:54:50.836 3399-3399/? W/System: ClassLoader referenced unknown path: /data/app/com.example.sloid.splashgun-1/lib/x86
12-04 06:54:50.844 3399-3399/? I/InstantRun: starting instant run server: is main process
12-04 06:54:50.878 3399-3399/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-04 06:54:51.018 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.018 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.023 3399-3399/? I/art: Alloc sticky concurrent mark sweep GC freed 5048(581KB) AllocSpace objects, 0(0B) LOS objects, 48% free, 4MB/8MB, paused 154us total 5.391ms
12-04 06:54:51.023 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.029 3399-3399/? I/art: Alloc partial concurrent mark sweep GC freed 101(4KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/7MB, paused 130us total 5.689ms
12-04 06:54:51.029 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.042 3399-3399/? I/art: Alloc concurrent mark sweep GC freed 6(168B) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/7MB, paused 137us total 12.522ms
12-04 06:54:51.042 3399-3399/? I/art: Forcing collection of SoftReferences for 105MB allocation
12-04 06:54:51.042 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.051 3399-3399/? I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/7MB, paused 144us total 8.744ms
12-04 06:54:51.051 3399-3399/? W/art: Throwing OutOfMemoryError "Failed to allocate a 110250012 byte allocation with 2981850 free bytes and 91MB until OOM"
12-04 06:54:51.051 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.051 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.054 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.059 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.065 3399-3399/? I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/7MB, paused 126us total 6.298ms
12-04 06:54:51.065 3399-3399/? I/art: Forcing collection of SoftReferences for 105MB allocation
12-04 06:54:51.065 3399-3399/? I/art: Starting a blocking GC Alloc
12-04 06:54:51.072 3399-3399/? I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/7MB, paused 116us total 6.824ms
12-04 06:54:51.072 3399-3399/? W/art: Throwing OutOfMemoryError "Failed to allocate a 110250012 byte allocation with 2981850 free bytes and 91MB until OOM"
12-04 06:54:51.072 3399-3399/? D/skia: --- allocation failed for scaled bitmap
12-04 06:54:51.073 3399-3399/? D/AndroidRuntime: Shutting down VM
12-04 06:54:51.075 3399-3399/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sloid.splashgun, PID: 3399
java.lang.OutOfMemoryError: Failed to allocate a 110250012 byte allocation with 2981850 free bytes and 91MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.loadDrawable(Resources.java:858)
at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
at android.widget.ImageView.<init>(ImageView.java:162)
at android.widget.ImageView.<init>(ImageView.java:150)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.sloid.splashgun.SplashScreen.onCreate(SplashScreen.java:18)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
您可以使用此代码执行您想要的操作。我在我的应用程序中使用它,如果问题不在于你的MainActivity,那么它可以正常工作
public class SplashActivity extends Activity implements Runnable {
Thread mThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashScreen);
mThread = new Thread(this);
mThread.start();
}
@Override
public void run(){
try {
Thread.sleep(8000);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
}
}
答案 1 :(得分:0)
您的代码看起来很好。错误是设备内存不足。其中一个原因可能是您的可绘制文件夹中的徽标文件太大。
您也可以尝试在具有更多可用内存的设备上运行此代码以进行确认。