启动画面

时间:2015-10-17 08:49:17

标签: java android splash-screen

我可以加载启动画面但3秒后应用程序崩溃。我想在启动画面后加载主要活动。我也发现之前在堆栈中讨论了相同的主题,但它没有帮助过我。

请帮我解决问题。

  

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a2.attemp2" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".SplashScreen"
        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=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.a2.MAINACTIVITY" />

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

</manifest>
  

SplashScreen.java

package com.example.a2.attemp2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        //Remove title bar
//          this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//
//        //Remove notification bar
//       // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//        //set content view AFTER ABOVE sequence (to avoid crash)
//        this.setContentView(R.layout.splash);


        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);

                    finish();
                }
            }
        };
        timerThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}
  

MainActivity.java

package com.example.a2.attemp2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I had recently started another project with same code with different package com.example.jarvis.digin I assure you that there is no compilation errors.
  

EVENT LOG

    10-17 21:46:20.887 7359-7359/com.example.jarvis.digin E/Trace: error opening trace file: No such file or directory (2)
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to resolve interface method 17898: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
10-17 21:46:21.347 7359-7359/com.example.jarvis.digin W/dalvikvm: VFY: unable to resolve interface method 17902: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
10-17 21:46:21.357 7359-7359/com.example.jarvis.digin D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin D/AndroidRuntime: Shutting down VM
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419a9300)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime: FATAL EXCEPTION: main
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jarvis.digin/com.example.jarvis.digin.SplashScreen}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:311)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:254)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.example.jarvis.digin.SplashScreen.onCreate(SplashScreen.java:17)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5008)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-17 21:46:21.457 7359-7359/com.example.jarvis.digin E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

3 个答案:

答案 0 :(得分:0)

 new android.os.Handler().postDelayed(new Runnable() { 

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @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);


        finish();
        }
    }, 3000);

答案 1 :(得分:0)

日志中的错误说它是由以下原因造成的:

IllegalStateException:您需要对此活动使用Theme.AppCompat主题(或后代)。

我猜您在AndroidManifest.xml中指定的主题存在问题。尝试将其更改为:

机器人:主题=&#34; @android:风格/ Theme.Holo&#34;

查看How to change app default theme to a different app theme?

答案 2 :(得分:0)

这是在设备上运行时的错误消息

  

此活动已有窗口装饰提供的操作栏。   请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并进行设置   在您的主题中,windowActionBar为false以使用工具栏。

上述信息清楚地表明了问题。

我通过从MainActivity.java删除以下代码解决了这个问题。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

希望这个答案能帮助初学者选择空白模板。

相关问题